home *** CD-ROM | disk | FTP | other *** search
/ 500 MB Nyheder Direkte fra Internet 2 / 500 MB nyheder direkte fra internet CD 2.iso / start / data / text / ass.txt < prev    next >
Text File  |  1994-09-21  |  102KB  |  2,214 lines

  1.  
  2.            NUMBER SYSTEMS
  3.   Most humans use the decimal system, which consists of ten 
  4. digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), because humans have ten 
  5. fingers. The computer does not have fingers, so it prefers other 
  6. number systems instead. Here they are. . . . 
  7.  
  8.                Binary
  9.   Look at these powers of 2:
  10. 20 =  1
  11.  
  12. 21 =  2
  13.  
  14. 22 =  4
  15.  
  16. 23 =  8
  17.  
  18. 24 = 16
  19.  
  20. 25 = 32
  21.  
  22. 26 = 64
  23.   Now try an experiment. Pick your favorite positive integer, and 
  24. try to write it as a sum of powers of 2.
  25.   For example, suppose you pick 45; you can write it as 32+8+4+1. 
  26. Suppose you pick 74; you can write it as 64+8+2. Suppose you pick 
  27. 77. You can write it as 64+8+4+1. Every positive integer can be 
  28. written as a sum of powers of 2.
  29.   Let's put those examples in a table:
  30. Original  Written as sum   Does the sum contain . . . 
  31. number    of powers of 2   64?  32?  16?  8?   4?   2?   1?
  32. 45        32+8+4+1         no   yes  no   yes  yes  no   yes
  33. 74        64+8+2           yes  no   no   yes  no   yes  no
  34. 77        64+8+4+1         yes  no   no   yes  yes  no   yes
  35.   To write those numbers in the binary system, replace ``no'' by 
  36. 0 and ``yes'' by 1:
  37. Decimal systemBinary system
  38. 45          0101101 (or simply 101101)
  39. 74          1001010
  40. 77          1001101
  41.   The decimal system uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 
  42. and 9 and uses these columns:
  43. thousands      hundreds      tens      units
  44. For example, the decimal number 7105 means ``7 thousands + 1 
  45. hundred + 0 tens + 5 units''.
  46.   The binary system uses only the digits 0 and 1, and uses these 
  47. columns:
  48. sixty-fours      thirty-twos      sixteens      eights      fours      
  49. twos      units
  50.   For example, the binary number 1001101 means ``1 sixty-four + 0 
  51. thirty-twos + 0 sixteens + 1 eight + 1 four + 0 twos + 1 unit''. 
  52. In other words, it means seventy-seven.
  53.                                          In elementary school, 
  54. you were taught how to do arithmetic in the decimal system. You 
  55. had to memorize the addition and multiplication tables:
  56.          DECIMAL ADDITION
  57.  
  58.    0  1  2  3  4  5  6  7  8  9
  59.  ┌─────────────────────────────┐
  60. 0│ 0  1  2  3  4  5  6  7  8  9│
  61. 1│ 1  2  3  4  5  6  7  8  9 10│
  62. 2│ 2  3  4  5  6  7  8  9 10 11│
  63. 3│ 3  4  5  6  7  8  9 10 11 12│
  64. 4│ 4  5  6  7  8  9 10 11 12 13│
  65. 5│ 5  6  7  8  9 10 11 12 13 14│
  66. 6│ 6  7  8  9 10 11 12 13 14 15│
  67. 7│ 7  8  9 10 11 12 13 14 15 16│
  68. 8│ 8  9 10 11 12 13 14 15 16 17│
  69. 9│ 9 10 11 12 13 14 15 16 17 18│
  70.  └─────────────────────────────┘
  71.     
  72.       DECIMAL MULTIPLICATION
  73.     
  74.    0  1  2  3  4  5  6  7  8  9
  75.  ┌─────────────────────────────┐
  76. 0│ 0  0  0  0  0  0  0  0  0  0│
  77. 1│ 0  1  2  3  4  5  6  7  8  9│
  78. 2│ 0  2  4  6  8 10 12 14 16 18│
  79. 3│ 0  3  6  9 12 15 18 21 24 27│
  80. 4│ 0  4  8 12 16 20 24 28 32 36│
  81. 5│ 0  5 10 15 20 25 30 35 40 45│
  82. 6│ 0  6 12 18 24 30 36 42 48 54│
  83. 7│ 0  7 14 21 28 35 42 49 56 63│
  84. 8│ 0  8 16 24 32 40 48 56 64 72│
  85. 9│ 0  9 18 27 36 45 54 63 72 81│
  86.  └─────────────────────────────┘
  87.                                          In the binary system, 
  88. the only digits are 0 and 1, so the tables are briefer:
  89. BINARY ADDITION
  90.  
  91.    0  1
  92.  ┌─────┐
  93. 0│ 0  1│
  94. 1│ 1 10│because two is written "10" in binary
  95.  └─────┘
  96.  
  97. BINARY MULTIPLICATION
  98.  
  99.    0  1
  100.  ┌─────┐
  101. 0│ 0  0│
  102. 1│ 0  1│
  103.  └─────┘
  104.                                          If society had adopted 
  105. the binary system instead of the decimal system, you'd have been 
  106. spared many hours of memorizing!
  107.                                          Usually, when you ask 
  108. the computer to perform a computation, it converts your numbers 
  109. from the decimal system to the binary system, performs the 
  110. computation by using the binary addition and multiplication 
  111. tables, and then converts the answer from the binary system to 
  112. the decimal system, so you can read it. For example, if you ask 
  113. the computer to print 45+74, it will do this:
  114.  45 converted to binary is   101101
  115. +74 converted to binary is +1001010
  116.                             1110111 converted to decimal is 119
  117.                                
  118.                                because 1+1=10
  119.  
  120.   The conversion from decimal to binary and then back to decimal 
  121. is slow. But the computation itself (in this case, addition) is 
  122. quick, since the binary addition table is so simple. The only 
  123. times the computer must convert is during input (decimal to 
  124. binary) and output (binary to decimal). The rest of the execution 
  125. is performed quickly, entirely in binary.
  126.   You know fractions can be written in the decimal system, by 
  127. using these columns:
  128. units      point      tenths      hundredths      thousandths
  129. For example, 1 5/8 can be written as 1.625, which means ``1 unit 
  130. + 6 tenths + 2 hundredths + 5 thousandths''.
  131.   To write fractions in the binary system, use these columns 
  132. instead:
  133. units      point      halves      fourths      eighths
  134. For example, 1 5/8 is written in binary as 1.101, which means ``1 
  135. unit + 1 half + 0 fourths + 1 eighth''.
  136.   You know 1/3 is written in the decimal system as 0.3333333 . . 
  137. . , which unfortunately never terminates. In the binary system, 
  138. the situation is no better: 1/3 is written as 0.010101. . . .  
  139. Since the computer stores only a finite number of digits, it 
  140. cannot store 1/3 accurately ___ it stores only an approximation.
  141.   A more distressing example is 1/5. In the decimal system, it's 
  142. .2, but in the binary system it's .0011001100110011. . . .  So 
  143. the computer can't handle 1/5 accurately, even though a human 
  144. can.
  145.   Most of today's microcomputers and minicomputers are inspired 
  146. by a famous maxicomputer built by DEC and called the DECsystem-10 
  147. (or PDP-10). Though DEC doesn't sell the DECsystem-10 anymore, 
  148. its influence lives on!
  149.   Suppose you run this BASIC program on a DECsystem-10 computer:
  150. 10 PRINT "MY FAVORITE NUMBER IS";4.001-4
  151. 20 END
  152. The computer will try to convert 4.001 to binary. Unfortunately, 
  153. it can't be converted exactly; the computer's binary 
  154. approximation of it is slightly too small. The computer's final 
  155. answer to 4.001-4 is therefore slightly less than the correct 
  156. answer. Instead of printing MY FAVORITE NUMBER IS .001, the 
  157. computer will print MY FAVORITE NUMBER IS .000999987.
  158.   If your computer isn't a DECsystem-10, its approximation will 
  159. be slightly different. To test your computer's accuracy, try 
  160. 4.0001-4, and 4.00001-4, and 4.000001-4, etc. You might be 
  161. surprised at its answers.
  162.   Let's see how the DECsystem-10 handles this:
  163. 10 FOR X = 7 TO 193 STEP .1
  164. 20     PRINT X
  165. 30 NEXT X
  166. 40 END
  167. The computer will convert 7 and 193 to binary accurately, but 
  168. will convert .1 to binary only approximately; the approximation 
  169. is slightly too large. The last few numbers it should print are 
  170. 192.8, 192.9, and 193, but because of the approximation it will 
  171. print slightly more than 192.8, then slightly more than 192.9, 
  172. and then stop (since it is not allowed to print anything over 
  173. 193).
  174.   There are only two binary digits: 0 and 1. A binary digit is 
  175. called a bit. For example, .001100110011 is a binary 
  176. approximation of 1/5 that consists of twelve bits. A sixteen-bit 
  177. approximation of 1/5 would be .0011001100110011. A bit that is 1 
  178. is called turned on; a bit that is 0 is turned off. For example, 
  179. in the expression 11001, three bits are turned on and two are 
  180. off. We also say that three of the bits are set and two are 
  181. cleared.
  182.   All information inside the computer is coded, in the form of 
  183. bits:
  184. Part of the computerWhat a 1 bit isWhat a 0 bit is
  185. electric wire high voltage  low voltage
  186. punched paper tapea hole in the tapeno hole in the tape
  187. punched IBM carda hole in the cardno hole in the card
  188. magnetic drum a magnetized areaa non-magnetized area
  189. core memory   core magnetized clockwisecore magnetized 
  190. counterclockwise
  191. flashing lightthe light is onthe light is off
  192.   For example, to represent 11 on part of a punched paper tape, 
  193. the computer punches two holes close together. To represent 1101, 
  194. the computer punches two holes close together, and then another 
  195. hole farther away.
  196.                                                              Octal
  197.                                                      Octal is a 
  198. shorthand notation for binary:
  199. Octal                                                  Meaning
  200. 0                                                      000
  201. 1                                                      001
  202. 2                                                      010
  203. 3                                                      011
  204. 4                                                      100
  205. 5                                                      101
  206. 6                                                      110
  207. 7                                                      111
  208. Each octal digit stands for three bits. For example, the octal 
  209. number 72 is short for this:
  210. 111010
  211.  
  212.  7  2
  213.                                                      To convert a 
  214. binary integer to octal, divide the number into chunks of three 
  215. bits, starting at the right. For example, here's how to convert 
  216. 11110101 to octal:
  217.  11110101
  218.  
  219.  3  6  5
  220.                                                      To convert a 
  221. binary real number to octal, divide the number into chunks of 
  222. three bits, starting at the decimal point and working in both 
  223. directions:
  224.  10100001.10011
  225.  
  226.  2  4  1 . 4  6
  227.  
  228.                                                           Hexadecimal
  229.                                                      Hexadecimal 
  230. is another short-hand notation for binary:
  231. Hexadecimal                                                  
  232. Meaning
  233. 0                                                            0000
  234. 1                                                            0001
  235. 2                                                            0010
  236. 3                                                            0011
  237. 4                                                            0100
  238. 5                                                            0101
  239. 6                                                            0110
  240. 7                                                            0111
  241. 8                                                            1000
  242. 9                                                            1001
  243. A                                                            1010
  244. B                                                            1011
  245. C                                                            1100
  246. D                                                            1101
  247. E                                                            1110
  248. F                                                            1111
  249. For example, the hexadecimal number 4F is short for this:
  250. 01001111
  251.  
  252.   4   F
  253. To convert a binary number to hexadecimal, divide the number into 
  254. chunks of 4 bits, starting at the decimal point and working in 
  255. both directions:
  256.  11010110100.1111111
  257.  
  258.   6   B   4   .  F   E
  259.  
  260.  
  261.      CHARACTER CODES
  262.   To store a character in a string, the computer uses a code.
  263.  
  264.           ASCII
  265.   The most famous code is the American Standard Code for 
  266. Information Interchange (ASCII), which has 7 bits for each 
  267. character. Here are examples:
  268.                 ASCII code
  269. CharacterASCII codein hexadec'l
  270. space   0100000 20
  271. !       0100001 21
  272. "       0100010 22
  273. #       0100011 23
  274. $       0100100 24
  275. %       0100101 25
  276. &       0100110 26
  277. '       0100111 27
  278. (       0101000 28
  279. )       0101001 29
  280. *       0101010 2A
  281. +       0101011 2B
  282. ,       0101100 2C
  283. -       0101101 2D
  284. .       0101110 2E
  285. /       0101111 2F
  286. 0       0110000 30
  287. 1       0110001 31
  288. 2       0110010 32
  289. etc.
  290. 9       0111001 39
  291. :       0111010 3A
  292. ;       0111011 3B
  293. <       0111100 3C
  294. =       0111101 3D
  295. >       0111110 3E
  296. ?       0111111 3F
  297. @       1000000 40
  298. A       1000001 41
  299. B       1000010 42
  300. C       1000011 43
  301. etc.
  302. Z       1011010 5A
  303. [       1011011 5B
  304. \       1011100 5C
  305. ]       1011101 5D
  306. ^       1011110 5E
  307. _       1011111 5F
  308.   ``ASCII'' is pronounced ``ass key''.
  309.   Most terminals use 7-bit ASCII. Most microcomputers and the 
  310. PDP-11 use an ``8-bit ASCII'' formed by putting a 0 before 7-bit 
  311. ASCII.      
  312.   PDP-8 computers use mainly a ``6-bit ASCII'' formed by 
  313. eliminating 7-bit ASCII's leftmost bit, but they can also handle 
  314. an ``8-bit ASCII'' formed by putting a 1 before 7-bit ASCII.
  315.   PDP-10 computers use mainly 7-bit ASCII but can also handle a 
  316. ``6-bit ASCII'' formed by eliminating ASCII's second bit. For 
  317. example, the 6-bit ASCII code for the symbol $ is 0 00100.
  318.   CDC computers use a special CDC 6-bit code.
  319.                                                 EBCDIC
  320.                              Instead of using ASCII, IBM 
  321. mainframes use the Extended Binary-Coded-Decimal Interchange Code 
  322. (EBCDIC), which has 8 bits for each character. Here are examples:
  323.                                    EBCDIC code                   
  324. EBCDIC code
  325. Character                          in hexadecimal        
  326. Character                                                        
  327. in hexadecimal
  328. space                              40                    A       
  329. C1
  330. ¢                                  4A                    B       
  331. C2
  332. <                                  4C                    etc.
  333. (                                  4D                    I       
  334. C9
  335. +                                  4E                    J       
  336. D1
  337. │                                  4F                    K       
  338. D2
  339. &                                  50                    etc.
  340. !                                  5A                    R       
  341. D9
  342. $                                  5B                    S       
  343. E2
  344. *                                  5C                    T       
  345. E3
  346. )                                  5D                    etc.
  347. ;                                  5E                    Z       
  348. E9
  349. ¬                                  5F                    0       
  350. F0
  351. -                                  60                    1       
  352. F1
  353. /                                  61                    etc.
  354. ,                                  6B                    9       
  355. F9
  356. %                                  6C
  357. _                                  6D
  358. >                                  6E
  359. ?                                  6F
  360. :                                  7A
  361. #                                  7B
  362. @                                  7C
  363. '                                  7D
  364. =                                  7E
  365. "                                  7F
  366. ``EBCDIC'' is pronounced ``ebb sih Dick''.
  367.                              IBM 360 computers can also handle an 
  368. ``8-bit ASCII'', formed by copying ASCII's first bit after the 
  369. second bit. For example, the 8-bit ASCII code for the symbol $ is 
  370. 01000100. But IBM 370 computers (which are newer than IBM 360 
  371. computers) don't bother with ASCII: they stick strictly with 
  372. EBCDIC.
  373.                              80-column IBM cards use Hollerith 
  374. code, which resembles EBCDIC but has 12 bits instead of 8. 
  375. 96-column IBM cards use a 6-bit code that's an abridgement of the 
  376. Hollerith code.
  377.                              Here's a program in BASIC:
  378. 10 IF "9"<"A" THEN 100
  379. 20 PRINT "CAT"
  380. 30 STOP
  381. 100 PRINT "DOG"
  382. 110 END
  383. Which will the computer print: CAT or DOG? The answer depends on 
  384. whether the computer uses ASCII or EBCDIC.
  385.                              Suppose the computer uses 7-bit 
  386. ASCII. Then the code for ``9'' is hexadecimal 39, and the code 
  387. for ``A'' is hexadecimal 41. Since 39 is less than 41, the 
  388. computer considers ``9'' to be less than ``A'', so the computer 
  389. prints DOG.
  390.                              But if the computer uses EBCDIC 
  391. instead of ASCII, the code for ``9'' is hexadecimal F9, and the 
  392. code for ``A'' is hexadecimal C1; since F9 is greater than C1, 
  393. the computer considers ``9'' to be greater than ``A'', so the 
  394. computer prints CAT.
  395.  
  396.                                                  Bytes
  397.                              A byte usually means: eight bits. 
  398. For example, here's a byte: 10001011.
  399.                              For computers that use 7-bit ASCII, 
  400. programmers sometimes define a byte to be 7 bits instead of 8. 
  401. For computers that use 6-bit ASCII, programmers sometimes define 
  402. a byte to be 6 bits. So if someone tries to sell you a computer 
  403. whose memory can hold ``16,000 bytes'', he probably means 16,000 
  404. 8-bit bytes, but might mean 7-bit bytes or 6-bit bytes.
  405.  
  406.                                                 Nibbles
  407.                              A nibble is 4 bits. It's half of an 
  408. 8-bit byte. Since a hexadecimal digit stands for 4 bits, a 
  409. hexadecimal digit stands for a nibble.
  410.  
  411.            SEXY ASSEMBLER
  412.   In this chapter, you'll learn the fundamental concepts of 
  413. assembly language, quickly and easily.
  414.   Unfortunately, different CPU's have different assembly 
  415. languages.
  416.   I've invented an assembly language that combines the best 
  417. features of all the other assembly languages. My assembly 
  418. language is called SEXY ASS, because it's a Simple, EXcellent, 
  419. Yummy ASSembler.
  420.   After you study the mysteries of the SEXY ASS, you can easily 
  421. get your rear in gear and become the dominant master of the 
  422. assemblers sold for Apple, Radio Shack, IBM, DEC, etc. Mastering 
  423. them will become so easy that you'll say, ``Assembly language is 
  424. a piece of cheesecake!''
  425.  
  426.            Bytes in my ASS
  427.   Let's get a close-up view of the SEXY ASS. . . . 
  428.   CPU registers The computer's guts consist of two main parts: 
  429. the brain (which is called the CPU) and the main memory (which 
  430. consists of RAM and ROM).
  431.   Inside the CPU are many electronic boxes, called registers. 
  432. Each register holds several electrical signals; each signal is 
  433. called a bit; so each register holds several bits. Each bit is 
  434. either 1 or 0. A ``1'' represents a high voltage; a ``0'' 
  435. represents a low voltage. If the bit is 1, the bit is said to be 
  436. high or on or set or true; if the bit is 0, the bit is said to be 
  437. low or off or cleared or false.
  438.   The CPU's most important register is called the accumulator (or 
  439. A). In the SEXY ASS system, the accumulator consists of 8 bits, 
  440. which is 1 byte. (Later, I'll explain how to make the CPU handle 
  441. several bytes simultaneously; but the accumulator itself holds 
  442. only 1 byte.)
  443.   Memory locations Like the CPU, the main memory consists of 
  444. electronic boxes. The electronic boxes in the CPU are called 
  445. registers, but the electronic boxes in the main memory are called 
  446. memory locations instead. Because the main memory acts like a 
  447. gigantic post office, the memory locations are also called 
  448. addresses. In the SEXY ASS system, each memory location holds 1 
  449. byte. There are many thousands of memory locations; they're 
  450. numbered 0, 1, 2, 3, etc.
  451.   Number systems When using SEXY ASS, you can type numbers in 
  452. decimal, binary, or hexadecimal. (For SEXY ASS, octal isn't 
  453. useful.) For example, the number ``twelve'' is written ``12'' in 
  454. decimal, ``1100'' in binary, and ``C'' in hexadecimal. To 
  455. indicate which number system you're using, put a percent sign in 
  456. front of each binary number, and put a dollar sign in front of 
  457. each hexadecimal number. For example, in SEXY ASS you can write 
  458. the number ``twelve'' as either 12 or %1100 or $C. (In that 
  459. respect, SEXY ASS copies the 6502 assembly language, which also 
  460. uses the percent sign and the dollar sign.)
  461.                                          Most of the time, we'll 
  462. be using hexadecimal, so let's quickly review what hexadecimal is 
  463. all about. To count in hexadecimal, just start counting as you 
  464. learned in elementary school ($1, $2, $3, $4, $5, $6, $7, $8, 
  465. $9);but after $9, you continue counting by using the letters of 
  466. the alphabet ($A, $B, $C, $D, $E, and $F). After $F (which is 
  467. fifteen), you say $10 (which means sixteen), then say $11 (which 
  468. means seventeen), then $12, then $13, then $14, etc., until you 
  469. reach $19; then come $1A, $1B, $1C, $1D, $1E, and $1F. Then come 
  470. $20, $21, $22, etc., up to $29, then $2A, $2B, $2C, $2D, $2E, and 
  471. $2F. Then comes $30. Eventually, you get up to $99, then $9A, 
  472. $9B, $9C, $9D, $9E, and $9F. Then come $A0, $A1, $A2, etc., up to 
  473. $AF. Then come $B0, $B1, $B2, etc., up to $BF. You continue that 
  474. pattern, until you reach $FF. Get together with your friends, and 
  475. try counting up to $FF. (Don't bother pronouncing the dollar 
  476. signs.) Yes, you too can count like a pro!
  477.                                          Each hexadecimal digit 
  478. represents 4 bits. Therefore, an 8-bit byte requires two 
  479. hexadecimal digits. So a byte can be anything from $00 to $FF.
  480.                                          Main segment I said that 
  481. the main memory consists of thousands of memory locations, 
  482. numbered 0, 1, 2, etc. The most important part of the main memory 
  483. is called the main memory bank or main segment: that part 
  484. consists of 65,536 memory locations (64K), which are numbered 
  485. from 0 to 65,535. Programmers usually number them in hexadecimal; 
  486. the hexadecimal numbers go from $0000 from $FFFF. ($FFFF in 
  487. hexadecimal is the same as 65.535 in decimal.) Later, I'll 
  488. explain how to use other parts of the memory; but for now, let's 
  489. restrict our attention to just 64K main segment.
  490.                                          How to copy a byte 
  491. Here's a simple, one-line program, written in the SEXY ASS 
  492. assembly language:
  493.         LOAD    $7000
  494. It makes the computer copy one byte, from memory location $7000 
  495. to the accumulator. So after the computer obeys that instruction, 
  496. the accumulator will contain the same data as the memory 
  497. location. For example, if the memory location contains the byte 
  498. %01001111 (which can also be written as $4F), so will the 
  499. accumulator.
  500.                                          Notice the wide space 
  501. before and after the word LOAD. To make the wide space, press the 
  502. TAB key.
  503.                                          The word LOAD tells the 
  504. computer to copy from a memory location to the accumulator. The 
  505. opposite of the word LOAD is the word STORE: it tells the 
  506. computer to copy from the accumulator to a memory location. For 
  507. example, if you type ___ 
  508.         STORE   $7000
  509. the computer will copy a byte from the accumulator to memory 
  510. location $7000.
  511.                                          Problem: write an 
  512. assembly-language program that copies a byte from memory location 
  513. $7000 to memory location $7001. Solution: you must do it in two 
  514. steps. First, copy from memory location $7000 to the accumulator 
  515. (by using the word LOAD); then copy from the accumulator to 
  516. memory location $7001 (by using the word STORE). Here's the 
  517. program:
  518.         LOAD    $7000
  519.         STORE   $7001
  520.  
  521.              Arithmetic
  522.   If you say ___ 
  523.         INC
  524. the computer will increment (increase) the number in the 
  525. accumulator, by adding 1 to it. For example, if the accumulator 
  526. contains the number $25, and you then say INC, the accumulator 
  527. will contain the number $26. For another example, if the 
  528. accumulator contains the number $39, and you say INC, the 
  529. accumulator will contain the number $3A (because, in hexadecimal, 
  530. after 9 comes A).
  531.   Problem: write a program that increments the number that's in 
  532. location $7000; for example, if location $7000 contains $25, the 
  533. program should change that data, so that location $7000 contains 
  534. $26 instead. Solution: copy the number from location $7000 to the 
  535. accumulator, then increment the number, then copy it back to 
  536. location $7000. . . . 
  537.         LOAD    $7000
  538.         INC
  539.         STORE   $7000
  540.   That example illustrates the fundamental rule of 
  541. assembly-language programming, which is: to manipulate a memory 
  542. location's data, copy the data to the accumulator, manipulate the 
  543. accumulator, and then copy the revised data from the accumulator 
  544. to memory.
  545.   The opposite of INC is DEC: it decrements (decreases) the 
  546. number in the accumulator, by subtracting 1 from it.
  547.   If you say ___ 
  548.         ADD     $7000
  549. the computer will change the number in the accumulator, by adding 
  550. to it the number that was in memory location $7000. For example, 
  551. if the accumulator had contained the number $16, and memory 
  552. location $7000 had contained the number $43, the number in the 
  553. accumulator will change and become the sum, $59. The number in 
  554. memory location $7000 will remain unchanged: it will still be 
  555. $43.
  556.   Problem: find the sum of the numbers in memory locations $7000, 
  557. $7001, and $7002, and put that sum into memory location $7003. 
  558. Solution: copy the number from memory location $7000 to the 
  559. accumulator, then add to the accumulator the numbers from memory 
  560. locations $7001 and $7002, so that the accumulator to memory 
  561. location $7003. . . . 
  562.         LOAD    $7000
  563.         ADD     $7001
  564.         ADD     $7002
  565.         STORE   $7003
  566.   The opposite of ADD is SUB, which means SUBtract. If you say 
  567. SUB $7000, the computer will change the number in the 
  568. accumulator, by subtracting from it the number in memory location 
  569. $7000.
  570.  
  571.         Immediate addressing
  572.   If you say ___ 
  573.         LOAD     #$25
  574. the computer will put the number $25 into the accumulator. The 
  575. $25 is the data. In the instruction ``LOAD #$25'', the symbol 
  576. ``#'' tells the computer that the $25 is the data instead of 
  577. being a memory location.
  578.   If you were to omit the #, the computer would assume the $25 
  579. meant memory location $0025, and so the computer would copy data 
  580. from memory location $0025 to the accumulator.
  581.   An instruction that contains the symbol # is said to be an 
  582. immediate instruction; it is said to use immediate addressing. 
  583. Such instructions are unusual.
  584.   The more usual kind of instruction, which does not use the
  585. symbol #, is called a direct instruction.
  586.                                          Problem: change the 
  587. number in the accumulator, by adding $12 to it. Solution:
  588.         ADD     #$12
  589.                                          Problem: change the 
  590. number in memory location $7000, by adding $12 to that number. 
  591. Solution: copy the number from memory location $7000 to the 
  592. accumulator, add $12 to it, and then copy the sum back to the 
  593. memory location. . . . 
  594.         LOAD    $7000
  595.         ADD     #$12
  596.         STORE   $7000
  597.                                          Problem: make the 
  598. computer find the sum of $16 and $43, and put the sum into memory 
  599. location $7000. Solution: put $16 into the accumulator, add $43 
  600. to it, and then copy from the accumulator to memory location 
  601. $7000. . . . 
  602.         LOAD    #$16
  603.         ADD     #$43
  604.         STORE   $7000
  605.  
  606.                                                      Video RAM
  607.                                          The video RAM is part of 
  608. the computer's RAM, and holds a copy of what's on the screen.
  609.                                          For example, suppose 
  610. you're running a program that analyzes taxicabs, and the screen 
  611. (of your TV or monitor) shows information about various cabs. If 
  612. the upper-left corner of the screen shows the word CAB, the video 
  613. RAM contains the ASCII code numbers for the letters C, A, and B. 
  614. Since the ASCII code number for C is 67 (which is $43), and the 
  615. ASCII code number for A is 65 (which is $41), and the ASCII code 
  616. number for B is 66 (which is $42), the video RAM contains $43, 
  617. $41, and $42. The $43, $41, and $42 represent the word CAB.
  618.                                          Suppose that the video 
  619. RAM begins at memory location $6000. If the screen's upper-left 
  620. corner shows the word CAB, memory location $6000 contains the 
  621. code for C (which is $43); the next memory location ($6001) 
  622. contains the code for A (which is $41); and the next memory 
  623. location ($6002) contains the code for B (which is $42).
  624.                                          Problem: assuming that 
  625. the video RAM begins at location $6000, make the computer write 
  626. the word CAB onto the screen's upper-left corner. Solution: write 
  627. $43 into memory location $6000, write $41 into memory location 
  628. $6001, and write $42 into memory location $6002. . . . 
  629.         LOAD    #$43
  630.         STORE   $6000
  631.         LOAD    #$41
  632.         STORE   $6001
  633.         LOAD    #$42
  634.         STORE   $6002
  635.                                          The computer knows that 
  636. $43 is the code number for ``C''. When you're writing that 
  637. program, if you're too lazy to figure out the $43, you can simply 
  638. write ``C''; the computer will understand. So you can write the 
  639. program like this:
  640.         LOAD    #"C"
  641.         STORE   $6000
  642.         LOAD    #"A"
  643.         STORE   $6001
  644.         LOAD    #"B"
  645.         STORE   $6002
  646.   That's the solution if the video RAM begins at memory location 
  647. $6000. On your computer, the video RAM might begin at a different 
  648. memory location instead. To find out about your computer's video 
  649. RAM, look at the back of the technical manual that came with your 
  650. computer. There you'll find a memory map: it shows which memory 
  651. locations are used by the video RAM, which memory locations are 
  652. used by other RAM, and which memory locations are used by the 
  653. ROM.
  654.  
  655.                 Flags
  656.   The CPU contains flags. Here's how they work.
  657.   Carry flag A byte consists of 8 bits. The smallest number you 
  658. can put into a byte is %00000000. The largest number you can put 
  659. into a byte is %11111111, which in hexadecimal is $FF; in 
  660. decimal, it's 255.
  661.   What happens if you try to go higher than %11111111? To find 
  662. out, examine this program:
  663.         LOAD    #%10000001
  664.         ADD     #%10000010
  665. In that program, the top line puts the binary number %10000001 
  666. into the accumulator. The next line tries to add %10000010 to the 
  667. accumulator. But the sum, which is %100000011, contains 9 bits 
  668. instead of 8, and therefore can't fit into the accumulator.
  669.   The computer splits that sum into two parts: the left bit (1) 
  670. and the remaining bits (00000011). The left bit (1) is called the 
  671. carry bit; the remaining bits (00000011) are called the tail. 
  672. Since the tail contains 8 bits, it fits nicely into the 
  673. accumulator; so the computer puts it into the accumulator. The 
  674. carry bit is put into a special place inside the CPU; that 
  675. special place is called the carry flag.
  676.   So that program makes the accumulator become 00000011, and 
  677. makes the carry flag become 1.
  678.   Here's an easier program:
  679.         LOAD    #%1
  680.         ADD     #%10
  681. The top line puts %1 into the accumulator; so the accumulator's 8 
  682. bits are %00000001. The bottom line adds %10 to the number in the 
  683. accumulator; so the accumulator's 8 bits become %00000011. Since 
  684. the numbers involved in that addition were so small, there was no 
  685. need for a 9th bit ___ no need for a carry bit. To emphasize that 
  686. no carry bit was required, the carry flag automatically becomes 
  687. 0.
  688.   Here's the rule: if an arithmetic operation (such as ADD, SUB, 
  689. INC, or DEC) gives a result that's too long to fit into 8 bits, 
  690. the carry flag becomes 1; otherwise, the carry flag becomes 0.
  691.                                          Negatives The largest 
  692. number you can fit into a byte %11111111, which in decimal is 
  693. 255. Suppose you try to add 1 to it. The sum is %100000000, which 
  694. in decimal is 256. But since %100000000 contains 9 bits, it's too 
  695. long to fit into a byte. So the computer sends the leftmost bit 
  696. (the 1) to the carry flag, and puts the tail (the 00000000) into 
  697. the accumulator. As a result, the accumulator contains 0.
  698.                                          So in assembly language, 
  699. if you tell the computer to do %11111111+1 (which is 255+1), the 
  700. accumulator says the answer is 0 (instead of 256).
  701.                                          In assembly language, 
  702. %11111111+1 is 0. In other words, %11111111 solves the equation 
  703. x+1=0.
  704.                                          According to high school 
  705. algebra, the equation x+1=0 has this solution: x=-1. But we've 
  706. seen that in the assembly language, the equation x+1=0 has the 
  707. solution x=%11111111. Conclusion: in assembly language, -1 is the 
  708. same as %11111111.
  709.                                          Now you know that -1 is 
  710. the same as %11111111, which is 255. Yes, -1 is the same as 255. 
  711. Similarly, -2 is the same as 254; -3 is the same as 253; -4 is 
  712. the same as 252. Here's the general formula: -n is the same as 
  713. 256-n. (That's because 256 is the same as 0.)
  714.                                          %11111111 is 255 and is 
  715. also -1. Since -1 is a shorter name than 255, we say that 
  716. %11111111 is interpreted as -1. Similarly, %11111110 is 254 and 
  717. also -2; since -2 is a shorter name than 254, we say that 
  718. %11111110 is interpreted as -2. At the other extreme, %00000010 
  719. is 2 and is also -254; since 2 is a shorter name than -254, we 
  720. say that %11111110 is interpreted as 2. Here's the rule: if a 
  721. number is ``almost'' 256, it's interpreted as a negative number; 
  722. otherwise, it's interpreted as a positive number.
  723.                                          How high must a number 
  724. be, in order to be ``almost'' 256, and therefore to be 
  725. interpreted as a negative number? The answer is: if the number is 
  726. at least 128, it's interpreted as a negative number. Putting it 
  727. another way, if the number's leftmost bit is 1, it's interpreted 
  728. as a negative number.
  729.                                          That strange train of 
  730. reasoning leads to the following definition: a negative number is 
  731. a byte whose leftmost bit is 1.
  732.                                          A byte's leftmost bit is 
  733. therefore called the negative bit or the sign bit.
  734.                                          Flag register You've 
  735. seen that the CPU contains a register called the accumulator. The 
  736. CPU also contains a second register, called the flag register. In 
  737. the SEXY ASS system, the flag register contains 8 bits (one 
  738. byte). Each of the 8 bits in the flag register is called a flag; 
  739. so the flag register contains 8 flags.
  740.                                          Each flag is a bit: it's 
  741. either 1 or 0. If the flag is 1, the flag is said to be up or 
  742. raised or set. If the flag is 0, the flag is said to be down or 
  743. lowered or cleared.
  744.                                          One of the 8 flags is 
  745. the carry flag: it's raised (becomes 1) whenever an arithmetic 
  746. operation requires a 9th bit. (It's lowered whenever an 
  747. arithmetic operation does not require a 9th bit.)
  748.                                          Another one of the flags 
  749. is the negative flag: it's raised whenever the number in the 
  750. accumulator becomes negative. For example, if the accumulator 
  751. becomes %11111110 (which is -2), the negative flag is raised 
  752. (i.e. the negative flag becomes 1). It's lowered whenever the 
  753. number in the accumulator becomes non-negative.
  754.                                          Another one of the flags 
  755. is the zero flag: it's raised whenever the number in the 
  756. accumulator becomes zero. (It's lowered whenever the number in 
  757. the accumulator becomes non-zero.)
  758.                 Jumps
  759.   You can give each line of your program a name. For example, you 
  760. can give a line the name FRED. To do so, put the name FRED at the 
  761. beginning of the line, like this:
  762. FRED    LOAD    $7000
  763. The line's name (FRED) is at the left margin. The command itself 
  764. (LOAD    $7000) is indented by pressing the TAB key. In that 
  765. line, FRED is called the label, LOAD is called the operation or 
  766. mnemonic, and $7000 is called the address.
  767.   Languages such as BASIC let you say ``GO TO''. In assembly 
  768. language, you say ``JUMP'' instead of ``GO TO''. For example, to 
  769. make the computer GO TO the line named FRED, say:
  770.         JUMP    FRED
  771. The computer will obey: it will JUMP to the line named FRED.
  772.   You can say ___ 
  773.         JUMPN   FRED
  774. That means: JUMP to FRED, if the Negative flag is raised. So the 
  775. computer will JUMP to FRED if a negative number was recently put 
  776. into the accumulator. (If a non-negative number was recently put 
  777. into the accumulator, the computer will not jump to FRED.)
  778.   JUMPN means ``JUMP if the Negative flag is raised.'' JUMPC 
  779. means ``JUMP if the Carry flag is raised.'' JUMPZ means ``JUMP if 
  780. the Zero flag is raised.''
  781.   JUMPNL means ``JUMP if the Negative flag is Lowered.'' JUMPCL 
  782. means ``JUMP if the Carry flag is Lowered.'' JUMPZL means ``JUMP 
  783. if the Zero flag is Lowered.''
  784.   Problem: make the computer look at memory location $7000; if 
  785. the number in that memory location is negative, make the computer 
  786. jump to a line named FRED. Solution: copy the number from memory 
  787. location $7000 to the accumulator, to influence the Negative 
  788. flag; then JUMP if Negative. . . . 
  789.         LOAD    $7000
  790.         JUMPN   FRED
  791.   Problem: make the computer look at memory location $7000. If 
  792. the number in that memory location is negative, make the computer 
  793. print a minus sign in the upper-left corner of the screen; if the 
  794. number is positive instead, make the computer print a plus sign 
  795. instead; if the number is zero, make the computer print a zero. 
  796. Solution: copy the number from memory location $7000 to the 
  797. accumulator (by saying LOAD); then analyze that number (by using 
  798. JUMPN and JUMPZ); then LOAD the ASCII code number for either 
  799. ``+'' or ``-'' or ``0'' into the accumulator (whichever is 
  800. appropriate); finally copy that ASCII code number from the 
  801. accumulator to the video RAM (by saying STORE). . . . 
  802.         LOAD    $7000
  803.         JUMPN   NEGAT
  804.         JUMPZ   ZERO
  805.  
  806.         LOAD    #"+"
  807.         JUMP    DISPLAY
  808.  
  809. NEGAT   LOAD    #"-"
  810.         JUMP    DISPLAY
  811.  
  812. ZERO    LOAD    #"0"
  813.  
  814. DISPLAY STORE   $6000
  815.  
  816.                                                  Machine language
  817.                                          I've been explaining 
  818. assembly language. Machine language resembles assembly language; 
  819. what's the difference?
  820.                                          To find out, let's look 
  821. at a machine language called SEXY MACHO (because it's a Simple, 
  822. EXcellent, Yummy MACHine language Original).
  823.                                          SEXY MACHO resembles 
  824. SEXY ASS; here are the major differences. . . . 
  825.                                          In SEXY ASS assembly 
  826. language, you use words such as LOAD, STORE, INC, DEC, ADD, SUB, 
  827. and JUMP. Those words are called operations or mnemonics. In SEXY 
  828. MACHO machine language, you replace those words by code numbers: 
  829. the code number for LOAD is 1; the code number for STORE is 2; 
  830. INC is 3; DEC is 4; ADD is 5; SUB is 6; and JUMP is 7. The code 
  831. numbers are called the operation codes or op codes.
  832.                                          In SEXY ASS assembly 
  833. language, the symbol ``#'' indicates immediate addressing; a lack 
  834. of the symbol ``#'' indicates direct addressing instead. In SEXY 
  835. MACHO machine language, you replace the symbol ``#'' by the code 
  836. number 1; if you want direct addressing instead, you must use the 
  837. code number 0.
  838.                                          In SEXY MACHO, all code 
  839. numbers are hexadecimal.
  840.                                          For example, look at 
  841. this SEXY ASS instruction:
  842.         ADD     #$43
  843. To translate that instruction into SEXY MACHO machine language, 
  844. just replace each symbol by its code number. Since the code 
  845. number for ADD is 5, and the code number for # is 1, the SEXY 
  846. MACHO version of that line is:
  847. 5143
  848.                                          Let's translate STORE 
  849. $7003 into SEXY MACHO machine language. Since the code for STORE 
  850. is 2, and the code for direct addressing is 0, the SEXY MACHO 
  851. version of that command is:
  852. 207003
  853.                                          In machine language, you 
  854. can't use any words or symbols: you must use their code numbers 
  855. instead. To translate a program from assembly language to machine 
  856. language, you must look up the code number of each word or 
  857. symbol.
  858.                                          An assembler is a 
  859. program that makes the computer translate from assembly language 
  860. to machine language.
  861.                                          The CPU understands only 
  862. machine language: it understands only numbers. It does not 
  863. understand assembly language: it does not understand words and 
  864. symbols. If you write a program in assembly language, you must 
  865. buy an assembler, which translates your program from assembly 
  866. language to machine language, so that the computer can understand 
  867. it.
  868.                                          Since assembly language 
  869. uses English words (such as LOAD), assembly language seems more 
  870. ``human'' than machine language (which uses code numbers). Since 
  871. programmers are humans, programmers prefer assembly language over 
  872. machine language. Therefore, the typical programmer writes in 
  873. assembly language, and then uses an assembler to translate the 
  874. program to machine language, which is the language that the CPU 
  875. ultimately requires.
  876.   Here's how the typical assembly-language programmer works. 
  877. First, the programmer types the assembly-language program and 
  878. uses a word processor to help edit it. The word processor 
  879. automatically puts the assembly-language program onto a disk. 
  880. Next, the programmer uses the assembler to translate the 
  881. assembly-language program into machine language. The assembler 
  882. puts the machine-language version of the program onto the disk. 
  883. So now the disk contains two versions of the program: the disk 
  884. contains the original version (in assembly language) and also 
  885. contains the translated version (in machine language). The 
  886. original version (in assembly language) is called the source 
  887. code; the translated version (in machine language) is called the 
  888. object code. Finally, the programmer gives a command that makes 
  889. the computer copy the machine-language version (the object code) 
  890. from the disk to the RAM and run it.
  891.   Here's a tough question: how does the assembler translate 
  892. ``JUMP FRED'' into machine language? Here's the answer. . . . 
  893.   The assembler realizes that FRED is the name for a line in your 
  894. program. The assembler hunts through your program, to find out 
  895. which line is labeled FRED. When the assembler finds that line, 
  896. it analyzes that line, to figure out where that line will be in 
  897. the RAM after the program is translated into machine language and 
  898. running. For example, suppose the line that's labeled FRED will 
  899. become a machine-language line which, when the program is 
  900. running, will be in the RAM at memory location $2053. Then ``JUMP 
  901. FRED'' must be translated into this command: ``jump to the 
  902. machine-language line that's in the RAM at memory location 
  903. $2053''. So ``JUMP FRED'' really means:
  904.         JUMP    $2053
  905. Since the code number for JUMP is 7, and the  addressing isn't 
  906. immediate (and therefore has code 0 instead of 1), the 
  907. machine-language version of JUMP FRED is:
  908. 702053
  909.  
  910.                                                   System software
  911.                                          The computer's main 
  912. memory consists of RAM and ROM. In a typical computer, the first 
  913. few memory locations ($0000, $0001, $0002, etc.) are ROM: they 
  914. permanently contain a program called the bootstrap, which is 
  915. written in machine-language.
  916.                                          When you turn on the 
  917. computer's power switch, the computer automatically runs the 
  918. bootstrap program. If your computer uses disks, the bootstrap 
  919. program makes the computer start reading information from the 
  920. disk in the main drive. In fact, it makes the computer copy a 
  921. machine-language program from the disk to the RAM. The 
  922. machine-language program that it copies is called the DOS.
  923.                                          After the DOS has been 
  924. copied to the RAM, the computer starts running the DOS program. 
  925. The DOS program makes the computer print a message on the screen 
  926. (such as ``Welcome to CP/M'' or ``Welcome to MS-DOS'') and print 
  927. a symbol on the screen (such as ``A>'') and then wait for you to 
  928. type a command.
  929.                                          That whole procedure is 
  930. called bootstrapping (or booting up), because of the phrase 
  931. ``pull yourself up by your own bootstraps''. By using the 
  932. bootstrap program, the computer pulls itself up to new 
  933. intellectual heights: it becomes a CP/M machine or an MS-DOS 
  934. machine or an Apple DOS machine or a TRSDOS machine.
  935.                                          After booting up, you 
  936. can start writing programs in BASIC. But how does the computer 
  937. understand the BASIC words, such as PRINT, INPUT, IF, THEN, and 
  938. GO TO? Here's how. . . . 
  939.                                          While you're using 
  940. BASIC, the computer is running a machine-language program, that 
  941. makes the computer seem to understand BASIC. That 
  942. machine-language program, which is in the computer's ROM or RAM, 
  943. is called the BASIC language processor or BASIC interpreter. If 
  944. your computer uses Microsoft BASIC, the BASIC interpreter is a 
  945. machine-language program that was written by Microsoft 
  946. Incorporated (a ``corporation'' that consists of Bill Gates and 
  947. his pals).
  948.  
  949.                                                How assemblers differ
  950.                                          In a microcomputer, the 
  951. CPU is a single chip, called the microprocessor. The most popular 
  952. microprocessors are the 8088, the 68000, and the 6502.
  953.                                          The 8088, designed by 
  954. Intel, hides in the IBM PC and clones. (The plain version is 
  955. called the 8088; a souped-up version, called the 80286, is in the 
  956. IBM PC AT.)
  957.                                          The 68000, designed by 
  958. Motorola, hides in the computers that rely on mice: the Apple 
  959. Mac, Commodore Amiga, and Atari ST. (The plain version is called 
  960. the 68000; a souped-up version, called the 68020, is in the Mac 
  961. 2; an even fancier version, called the 68030, is in fancier 
  962. Macs.)
  963.                                          The 6502, designed by 
  964. MOS Technology (which has become part of Commodore), hides in 
  965. old-fashioned cheap computers: the Apple 2 family, the Commodore 
  966. 64 & 128, and the Atari XL & XE.
  967.                                          Let's see how their 
  968. assemblers differ from SEXY ASS.
  969.   Number systems SEXY ASS assumes all numbers are written in the 
  970. decimal system, unless preceded by a dollar sign (which means 
  971. hexadecimal) or percent sign (which means binary).
  972.   68000 and 6502 assemblers resemble SEXY ASS, except that they 
  973. don't understand percent signs and binary notation. Some 
  974. stripped-down 6502 assemblers don't understand the decimal system 
  975. either: they require all numbers to be in hexadecimal.
  976.   The 8088 assembler comes in two versions.
  977.   The full version of the 8088 assembler is called the Microsoft 
  978. Macro ASseMbler (MASM). It lists for $150, but discount dealers 
  979. sell it for just $83. It assumes all numbers are written in the 
  980. decimal system, unless followed by an H (which means hexadecimal) 
  981. or B (which means binary). For example, the number twelve can be 
  982. written as 12 or as 0CH or as 1100B. It requires each number to 
  983. begin with a digit: so to say twelve in hexadecimal, instead of 
  984. saying CH you must say 0CH.
  985.   A stripped-down 8088 assembler, called the DEBUG 
  986. mini-assembler, is part of DOS; so you get it at no extra charge 
  987. when you buy DOS. It requires all numbers to be written in 
  988. hexadecimal. For example, it requires the number twelve to be 
  989. written as C. Do not put a dollar sign or H next to the C.
  990.   Accumulator Each microprocessor contains several accumulators, 
  991. so you must say which accumulator to use. The main 8-bit 
  992. accumulator is called ``A'' in the 6502, ``AL'' in the 8088, and 
  993. ``D0.B'' in the 68000.
  994.   Labels SEXY ASS and the other full assemblers let you begin a 
  995. line with a label, such as FRED. For the 8088 full assembler 
  996. (MASM), add a colon after FRED. Mini-assemblers (such as 8088 
  997. DEBUG) don't understand labels.
  998.   Commands Here's how to translate from SEXY ASS to the popular 
  999. assemblers:
  1000. Computer's actionSEXY ASS650268000    8088 MASM
  1001. put 25 in accumulatorLOAD #$25LDA #$25MOVE.B #$25,D0MOV AL,25H
  1002. copy location 7000 to acc.LOAD $7000LDA $7000MOVE.B $7000,D0MOV 
  1003. AL,[7000H]
  1004. copy acc. to location 7000STORE $7000STA $7000MOVE.B D0,$7000MOV 
  1005. [7000H],AL
  1006. add location 7000 to acc.ADD $7000ADC $7000ADD.B $7000,D0ADD 
  1007. AL,[7000H]
  1008. subtract loc. 7000 from acc.SUB $7000SBC $7000SUB.B $7000,D0SUB 
  1009. AL,[7000H]
  1010. increment accumulatorINCADC #$1ADDQ.B #1,D0INC AL
  1011. decrement accumulatorDECSBC #$1SUBQ.B #1,D0DEC AL
  1012. put character C in acc.LOAD #"C"LDA #'CMOVE.B #'C',D0MOV AL,"C"
  1013. jump to FRED  JUMP FREDJMP FREDJMP FREDJMP FRED
  1014. jump if negativeJUMPN FREDBMI FREDBMI FREDJS FRED
  1015. jump if carry JUMPC FREDBCS FREDBCS FREDJC FRED
  1016. jump if zero  JUMPZ FREDBEQ FREDBEQ FREDJZ FRED
  1017. jump if negative loweredJUMPNL FREDBPL FREDBPL FREDJNS FRED
  1018. jump if carry loweredJUMPCL FREDBCC FREDBCC FREDJNC FRED
  1019. jump if zero loweredJUMPZL FREDBNE FREDBNE FREDJNZ FRED
  1020.   Notice that in 6502 assembler, each mnemonic (such as LDA) is 
  1021. three characters long.
  1022.   To refer to an ASCII character, SEXY ASS and 8088 MASM put the 
  1023. character in quotes, like this: ``C''. 68000 assembler uses 
  1024. apostrophes instead, like this: 'C'. 6502 assembler uses just a 
  1025. single apostrophe, like this: 'C.
  1026.   Instead of saying ``jump if'', 6502 and 68000 programmers say 
  1027. ``branch if'' and use mnemonics that start with B instead of J. 
  1028. For example, they use mnemonics such as BMI (which means ``Branch 
  1029. if MInus''), BCS (``Branch if Carry Set''), and BEQ (``Branch if 
  1030. EQual to zero'').
  1031.   To make the 68000 manipulate a byte, put ``.B'' after the 
  1032. mnemonic. (If you say ``.W'' instead, the computer will 
  1033. manipulate a 16-bit word instead of a byte. If you say ``.L'' 
  1034. instead, the computer will manipulate long data containing 32 
  1035. bits. If you don't specify ``.B'' or ``.W'' or ``.L'', the 
  1036. assembler assumes you mean ``.W''.)
  1037.   8088 assemblers require you to put each memory location in 
  1038. brackets. So whenever you refer to location 7000 hexadecimal, you 
  1039. put the 7000H in brackets, like this: [7000H].
  1040.  
  1041.           DEBUG
  1042.                                                      When you buy 
  1043. PC-DOS for your IBM PC (or MS-DOS for your clone), you get a disk 
  1044. that contains many DOS files. One of the DOS files is called 
  1045. DEBUG. It helps you debug your software and hardware.
  1046.                                                      It lets you 
  1047. type special debugger commands. It also lets you type commands in 
  1048. assembly language.
  1049.  
  1050.                                                          How to start
  1051.                                                      Put the main 
  1052. DOS disk in drive A, then turn the computer on. Type the date and 
  1053. time.
  1054.                                                      Press the 
  1055. CAPS LOCK key, so that everything you type will be capitalized. 
  1056. If you're using PC-DOS instead of MS-DOS, put the disk marked 
  1057. ``DOS Supplemental Programs'' into drive A.
  1058.                                                      After the A 
  1059. prompt, type the word DEBUG, so your screen looks like this:
  1060. A>DEBUG
  1061. When you press the ENTER key after DEBUG, the computer will print 
  1062. a hyphen, like this:
  1063. -
  1064. After the hyphen, you can give any DEBUG command.
  1065.  
  1066.                                                            Registers
  1067.                                                      To see 
  1068. what's in the CPU registers, type an R after the hyphen, so your 
  1069. screen looks like this:
  1070. -R
  1071. When you press the ENTER key after the R, the computer will 
  1072. print:
  1073. AX=0000  BX=0000  CX=0000  DX=0000
  1074. That means the main registers (which are called AX, BX, CX, and 
  1075. DX) each contain hexadecimal 0000. Then the computer will tell 
  1076. you what's in the other registers, which are called SP, BP, SI, 
  1077. DI, DS, ES, SS, CS, IP, and FLAGS. Finally, the computer will 
  1078. print a hyphen, after which you can type another command.
  1079.                                                      Editing the 
  1080. registers To change what's in register BX, type RBX after the 
  1081. hyphen, so your screen looks like this:
  1082. -RBX
  1083.                                                      The computer 
  1084. will remind you of what's in register BX, by saying:
  1085. BX 0000
  1086. :
  1087. To change BX to hexadecimal 7251, type 7251 after the colon, so 
  1088. your screen looks like this:
  1089. :7251
  1090.  
  1091. That makes the computer put 7251 into register BX.
  1092.   To see that the computer put 7251 into register BX, say:
  1093. -R
  1094. That makes the computer tell you what's in all the registers. It 
  1095. will begin by saying:
  1096. AX=0000  BX=7251  CX=0000  DX=0000
  1097.   Experiment! Try putting different hexadecimal numbers into the 
  1098. registers! To be safe, use just the registers AX, BX, CX, and DX.
  1099.   Segment registers The computer's RAM is divided into segments. 
  1100. The segment registers (DS, ES, SS, and CS) tell the computer 
  1101. which segments to use.
  1102.   Do not change the numbers in the segment registers! Changing 
  1103. them will make the computer use the wrong segments of the RAM and 
  1104. wreck your DOS and disks.
  1105.   The CS register is called the code segment register. It tells 
  1106. the computer which RAM segment to put your programs in. For 
  1107. example, if the CS register contains the hexadecimal number 0E9F, 
  1108. the computer will put your programs in segment number 0E9F.
  1109.  
  1110.        DEBUG's mini-assembler
  1111.   To use assembly language, type A100 after the hyphen, so your 
  1112. screen looks like this:
  1113. -A100
  1114.   The computer will print the code segment number, then a colon, 
  1115. then 0100. For example, if the code segment register contains the 
  1116. hexadecimal number 0E9F, the computer will print:
  1117. 0E9F:0100
  1118.   Now you can type an assembly-language program!
  1119.   For example, suppose you want to move the hexadecimal number 
  1120. 2794 to register AX and move 8156 to BX. Here's the 
  1121. assembly-language program:
  1122. MOV AX,2794
  1123. MOV BX,8156
  1124. Type that program. As you type it, the computer will 
  1125. automatically put a segment number and memory location in front 
  1126. of each line, so your screen will look like this:
  1127. 0E9F:0100 MOV AX,2794
  1128. 0E9F:0103 MOV BX,8156
  1129. 0E9F:0106
  1130. After the 0E9F:0106, press the ENTER key. The computer will stop 
  1131. using assembly language and will print a hyphen.
  1132.   After the hyphen, type G=100 106, so your screen looks like 
  1133. this:
  1134. -G=100 106
  1135. That tells the computer to run your assembly-language program, 
  1136. going from location 100 to location 106, so the computer will 
  1137. start at location 100 and stop when it reaches memory location 
  1138. number 106.
  1139.   After running the program, the computer will tell you what's in 
  1140. the registers. It will print:
  1141. AX=2794  BX=8156  CX=0000  DX=0000
  1142. It will also print the numbers in all the other registers.
  1143.                                          Listing your program To 
  1144. list your program, type U100 after the hyphen, so your screen 
  1145. looks like this:
  1146. -U100
  1147. The U stands for ``Unassemble'', which means ``list''. The 
  1148. computer will list your program, beginning at line 100. The 
  1149. computer will begin by saying:
  1150. 0E9F:0100 B89427     MOV   AX,2794
  1151. 0E9F:0103 BB5681     MOV   BX,8156
  1152.                                          The top line consists of 
  1153. three parts. The left part (0E9F:0100) is the address in memory. 
  1154. The right part (MOV AX, 2794) is the assembly-language 
  1155. instruction beginning at that address.
  1156.                                          The middle part (B89427) 
  1157. is the machine-language translation of MOV AX,2794. That middle 
  1158. part begins with B8, which is the machine-language translation of 
  1159. MOV AX. Then comes 9427, which is the machine-language 
  1160. translation of 2794; notice how machine language puts the digits 
  1161. in a different order than assembly language.
  1162.                                          The machine-language 
  1163. version, B89427, occupies three bytes of RAM. The first byte 
  1164. (address 0100) contains the hexadecimal number B8; the next byte 
  1165. (address 0101) contains the hexadecimal number 94; the final byte 
  1166. (address 0102) contains the hexadecimal number 27.
  1167.                                          So altogether, the 
  1168. machine-language version of MOV AX,2794 occupies addresses 0100, 
  1169. 0101, and 0102. That's why the next instruction (MOV BX,8156) 
  1170. begins at address 0103.
  1171.                                          After the computer 
  1172. prints that analysis of your program, the computer will continue 
  1173. by printing an analysis of the next several bytes of memory also. 
  1174. Altogether, the computer will print an analysis of addresses up 
  1175. through 011F. What's in those addresses depends on which program 
  1176. your computer was running before you ran this one.
  1177.                                          Editing your program To 
  1178. edit line 0103, type:
  1179. -A103
  1180. Then type the assembly-language command you want for location 
  1181. 103.
  1182.                                          When you finish the 
  1183. command and press the ENTER key, the computer will give you an 
  1184. opportunity to edit the next line (106). If you don't want to 
  1185. edit or create a line 106, press the ENTER key again.
  1186.                                          After editing your 
  1187. program, list it (by typing U100), to make sure you edited 
  1188. correctly.
  1189.                                          Arithmetic This 
  1190. assembly-language program does arithmetic:
  1191. MOV AX,7
  1192. ADD AX,5
  1193. To feed that program to the computer, say A100 after the hyphen, 
  1194. then type the program, then press the ENTER key an extra time, 
  1195. then say G=100 106.
  1196.                                          That program's top line 
  1197. moves the number 7 into the AX register. The next line adds 5 to 
  1198. the AX register, so the number in the AX register becomes twelve. 
  1199. In hexadecimal, twelve is written as C, so the computer will say:
  1200. AX=000C
  1201. The computer will also say what's in the other registers.
  1202.                                          The opposite of ADD is 
  1203. SUB, which means subtract. For example, if you say ___ 
  1204. SUB AX,3
  1205. the computer will subtract 3 from the number in the AX register, 
  1206. so the number in the AX register becomes smaller.
  1207.   To add 1 to the number in the AX register, you can say:
  1208. ADD AX,1
  1209. For a short cut, say this instead:
  1210. INC AX
  1211. That tells the computer to INCrement the AX register, by adding 
  1212. 1.
  1213.   To subtract 1 from the number in the AX register, you can say:
  1214. SUB AX,1
  1215. For a short cut, say this instead ___ 
  1216. DEC AX
  1217. which means ``DECrement the AX register''.
  1218.   Half registers A register's left half is called the high part. 
  1219. The register's right half is called the low part.
  1220.   For example, if the AX register contains 9273, the register's 
  1221. high part is 92, and the low part is 73.
  1222.   The AX register's high part is called ``A high'' or AH. The AX 
  1223. register's low part is called ``A low'' or AL.
  1224.   Suppose the AX register contains 9273 and you say:
  1225. MOV AH,41
  1226. The computer will make AX's high part be 41, so AX becomes 4173.
  1227.   Copying to memory Let's program the computer to put the 
  1228. hexadecimal number 52 into memory location 7000.
  1229.   This command almost works:
  1230. MOV [7000],52
  1231. In that command, the brackets around the 7000 mean ``memory 
  1232. location''. That command says to move, into location 7000, the 
  1233. number 52.
  1234.   Unfortunately, if you type that command, the computer will 
  1235. gripe, because the computer can't handle two numbers 
  1236. simultaneously (7000 and 52).
  1237.   Instead, you split that complicated command into two simpler 
  1238. commands, each involving just one number. Instead of trying to 
  1239. move 52 directly into location 7000, first move 52 into a 
  1240. register (such as AL), then copy that register into location 
  1241. 7000, like this:
  1242. MOV AL,52
  1243. MOV [7000],AL
  1244.   After running that program, you can prove the 52 got into 
  1245. location 7000, by typing:
  1246. -E7000
  1247. That makes the computer examine location 7000. The computer will 
  1248. find 52 there and print:
  1249. 0E9F:7000 52.
  1250. That means: segment 0E9F's 7000th location contains 52.
  1251.   If you change your mind and want it to contain 53 instead, type 
  1252. 53 after the period.
  1253.   Next, press the ENTER key, which makes the computer print a 
  1254. hyphen, so you can give your next DEBUG command.
  1255.   Interrupt 21 Here's how to write an assembly-language program 
  1256. that prints the letter C on the screen.
  1257.   The ASCII code number for ``C'' is hexadecimal 43. Put 43 into 
  1258. the DL register:
  1259. 0E9F:0100 MOV DL,43
  1260.   The DOS code number for ``screen output'' is 2. Put 2 into the 
  1261. AH register:
  1262. 0E9F:0102 MOV AH,2
  1263.  
  1264.                                          To make the computer use 
  1265. the code numbers you put into the DL and AH registers, tell the 
  1266. computer to do DOS interrupt subroutine #21:
  1267. 0E9F:0104 INT 21
  1268.                                          So altogether, the 
  1269. program looks like this:
  1270. 0E9F:0100 MOV DL,43
  1271. 0E9F:0102 MOV AH,2
  1272. 0E9F:0104 INT 21
  1273. 0E9F:0106
  1274.                                          To make the computer do 
  1275. that program, say G=100 106. The computer will obey the program, 
  1276. so your screen will say:
  1277. C
  1278.                                          After running the 
  1279. program, the computer will tell you what's in all the registers. 
  1280. You'll see that DL has become 43 (because of line 100), AH has 
  1281. become 02 (because of line 102), and AL has become 43 (because 
  1282. INT 21 automatically makes the computer copy DL to AL). Then the 
  1283. computer will print a hyphen, so you can give another DEBUG 
  1284. command.
  1285.                                          Instead of printing just 
  1286. C, let's make the computer print CCC. Here's how. Put the code 
  1287. numbers for ``C'' and ``screen output'' into the registers:
  1288. 0E9F:0100 MOV DL,43
  1289. 0E9F:0102 MOV AH,02
  1290. Then tell DOS to use those code numbers, three times:
  1291. 0E9F:0104 INT 21
  1292. 0E9F:0106 INT 21
  1293. 0E9F:0108 INT 21
  1294. 0E9F:010A
  1295. To run that program, say G=100 10A. The computer will print:
  1296. CCC
  1297.                                          Jumps Here's how to make 
  1298. the computer print C repeatedly, so that the entire screen gets 
  1299. filled with C's.
  1300.                                          Put the code numbers for 
  1301. ``C'' and ``screen output'' into the registers:
  1302. 0E9F:0100 MOV DL,43
  1303. 0E9F:0102 MOV AH,02
  1304.                                          In line 104, tell DOS to 
  1305. use those code numbers:
  1306. 0E9F:0104 INT 21
  1307. To create a loop, jump back to line 104:
  1308. 0E9F:0106 JMP 104
  1309.                                          Altogether, the program 
  1310. looks like this:
  1311. 0E9F:0100 MOV DL,43
  1312. 0E9F:0102 MOV AH,03
  1313. 0E9F:0104 INT 21
  1314. 0E9F:0106 JMP 104
  1315. 0E9F:0108
  1316.                                          To run that program, say 
  1317. G=100 108. The computer will print C repeatedly, so the whole 
  1318. screen gets filled with C's. To abort the program, tap the BREAK 
  1319. key while holding down the CONTROL key.
  1320.                                          Interrupt 20 I showed 
  1321. you this program, which makes the computer print the letter C:
  1322. 0E9F:0100 MOV DL,43
  1323. 0E9F:0102 MOV AH,2
  1324. 0E9F:0104 INT 21
  1325. 0E9F:0106
  1326. If you run that program by saying G=100 106, the computer will 
  1327. print C and then tell you what's in all the registers.
  1328.                                          Instead of making the 
  1329. computer tell you what's in all the registers, let's make the 
  1330. computer say:
  1331. Program terminated normally
  1332.  
  1333.   To do that, make the bottom line of your program say INT 20, 
  1334. like this:
  1335. 0E9F:0100 MOV DL,43
  1336. 0E9F:0102 MOV AH,2
  1337. 0E9F:0104 INT 21
  1338. 0E9F:0106 INT 20
  1339. 0E9F:0108
  1340.   The INT 20 makes the computer print ``Program terminated 
  1341. normally'' and then end, without printing a message about the 
  1342. registers.
  1343.   To run the program, just say G=100. You do not have to say 
  1344. G=100 108, since the INT 20 ends the program before the computer 
  1345. reaches 108 anyway. The program makes the computer print:
  1346. C
  1347. Program terminated normally
  1348.   Strings This program makes the computer print the string ``I 
  1349. LOVE YOU'':
  1350. 0E9F:0100 MOV DX,109
  1351. 0E9F:0103 MOV AH,9
  1352. 0E9F:0105 INT 21
  1353. 0E9F:0107 INT 20
  1354. 0E9F:0109 DB "I LOVE YOU$"
  1355. 0E9F:0114
  1356.   The bottom line contains the string to be printed: ``I LOVE 
  1357. YOU$''. Notice you must end the string with a dollar sign. In 
  1358. that line, the DB stands for Define Bytes.
  1359.   Here's how the program works. The top line puts the string's 
  1360. line number (109) into DX. The next line puts 9, which is the 
  1361. code number for ``string printing'', into AH. The next line (INT 
  1362. 21) makes the computer use the line number and code number to do 
  1363. the printing. The next line (INT 20) makes the program print 
  1364. ``Program terminated normally'' and end.
  1365.   When you run the program (by typing G=100), the computer will 
  1366. print:
  1367. I LOVE YOU
  1368. Program terminated normally
  1369.   If you try to list the program by saying U100, the listing will 
  1370. look strange, because the computer can't list the DB line 
  1371. correctly. But even though the listing will look strange, the 
  1372. program will still run fine.
  1373.   Saving your program After you've created an assembly-language 
  1374. program, you can copy it onto a disk and save it. Here's how.
  1375.   First, make sure the program ends by saying INT 20, so that the 
  1376. program terminates normally.
  1377.   Next, invent a name for the program. The name should end in 
  1378. .COM. For example, to give your program the name LOVER.COM, type 
  1379. this:
  1380. -NLOVER.COM
  1381.   Put 0 into register BX (by typing -RBX and then :0).
  1382.   Put the program's length into register CS. For example, since 
  1383. the program above starts at line 0100 and ends at line 0114 
  1384. (which is blank), the program's length is ``0114 minus 0100'', 
  1385. which is 14; so put 14 into register CX (by typing -RCX and then 
  1386. :14).
  1387.   Finally, say -W, which makes the computer write the program 
  1388. onto the disk. The computer will say:
  1389. Writing 0014 bytes
  1390.  
  1391.                                                      Quitting
  1392.                                          When you finish using 
  1393. DEBUG, tell the computer to quit, by typing a Q after the hyphen. 
  1394. When you press the ENTER key after the Q, the computer will quit 
  1395. using DEBUG and say:
  1396. A>
  1397.                                          After quitting, you can 
  1398. give any DOS command you wish. If you used assembly language to 
  1399. create a program called LOVER.COM, you can run it by just typing:
  1400. A>LOVER
  1401. The computer will run the program and say:
  1402. I LOVE YOU
  1403. Then the computer will print ``A>'', so you can give another DOS 
  1404. command.
  1405.                                          Notice that the computer 
  1406. doesn't bother to print a message saying ``Program terminated 
  1407. normally''. (It prints that message only when you're in the 
  1408. middle of using DEBUG.)
  1409.                                          Now you know how to 
  1410. write assembly-language programs. Dive in! Write your own 
  1411. programs!
  1412.  
  1413.      INSIDE THE CPU
  1414.   Let's peek inside the CPU and see what lurks within!
  1415.  
  1416.      Program counter
  1417.   Each CPU contains a special register called the program 
  1418. counter.
  1419.   The program counter tells the CPU which line of your program to 
  1420. do next. For example, if the program counter contains the number 
  1421. 6 (written in binary), the CPU will do the line of your program 
  1422. that's stored in the 6th memory location.
  1423.   More precisely, here's what happens if the program counter 
  1424. contains the number 6. . . . 
  1425.   A. The CPU moves the content of the 6th memory location to the 
  1426. CPU's instruction register. (That's called fetching the 
  1427. instruction.)
  1428.   B. The CPU checks whether the instruction register contains a 
  1429. complete instruction written in machine language. If not ___ if 
  1430. the instruction register contains only part of a machine-language 
  1431. instruction ___ the CPU fetches the content of the 7th memory 
  1432. location also. (The instruction register is large enough to hold 
  1433. the content of memory locations 6 and 7 simultaneously.) If the 
  1434. instruction register still doesn't contain a complete 
  1435. instruction, the CPU fetches the content of the 8th memory 
  1436. location also. If the instruction register still doesn't contain 
  1437. a complete instruction, the CPU fetches the content of the 9th 
  1438. memory location also.
  1439.   C. The CPU changes the number in the program counter. For 
  1440. example, if the CPU has fetched from the 6th and 7th memory 
  1441. locations, it makes the number in the program counter be 8; if 
  1442. the CPU has fetched from the 6th, 7th, and 8th memory locations, 
  1443. it makes the number in the program counter be 9. (That's called 
  1444. updating the program counter.)
  1445.   D. The CPU figures out what the instruction means. (That's 
  1446. called decoding the instruction.)
  1447.   E. The CPU obeys the instruction. (That's called executing the 
  1448. instruction.) If it's a ``GO TO'' type of instruction, the CPU 
  1449. makes the program counter contain the address of the memory 
  1450. location you want to go to.
  1451.                              After the CPU completes steps A, B, 
  1452. C, D, and E, it looks at the program counter and moves on to the 
  1453. next instruction. For example, if the program counter contains 
  1454. the number 9 now, the CPU does steps A, B, C, D, and E again, but 
  1455. by fetching, decoding, and executing the 9th memory location 
  1456. instead of the 6th.
  1457.                              The CPU repeats steps A, B, C, D, 
  1458. and E again and again; each time, the number in the program 
  1459. counter changes. Those five steps form a loop, called the 
  1460. instruction cycle.
  1461.  
  1462.                                          Arithmetic/logic unit
  1463.                              The CPU contains two parts: the 
  1464. control unit (which is the boss) and the arithmetic/logic unit 
  1465. (ALU). When the control unit comes to step D of the instruction 
  1466. cycle, and decides some arithmetic or logic needs to be done, it 
  1467. sends the problem to the ALU, which sends back the answer.
  1468.                              Here's what the ALU can do:
  1469. Name of operation                        Example Explanation
  1470. plus, added to, +                         10001010add, but 
  1471. remember that 1+1 is 10 in binary
  1472.                                          +10001001
  1473.                                          100010011
  1474.  
  1475. minus, subtract, -                        10001010subtract, but 
  1476. remember 10-1 is 1 in binary
  1477.                                          -10001001
  1478.                                           00000001
  1479.  
  1480. negative, -,                             -10001010left of the 
  1481. rightmost 1, do this:
  1482. the two's complement of                   01110110replace each 0 
  1483. by 1, and each 1 by 0
  1484.  
  1485. not, ~, the complement of,               ~10001010replace each 0 
  1486. by 1, and each 1 by 0
  1487. the one's complement of                   01110101
  1488.  
  1489. and, &, ^                                 10001010put 1 wherever 
  1490. both original numbers had 1
  1491.                                          ^ 10001001
  1492.                                           10001000
  1493.  
  1494. or, inclusive or, v                       10001010put 1 wherever 
  1495. some original number had 1
  1496.                                          v 10001001
  1497.                                           10001011
  1498.  
  1499. eXclusive OR, XOR, v~                     10001010put 1 wherever 
  1500. the original numbers differ
  1501.                                          v~10001001
  1502.                                           00000011
  1503.                              Also, the ALU can shift a register's 
  1504. bits. For example, suppose a register contains 10111001. The ALU 
  1505. can shift the bits toward the right:
  1506. before                         10111001
  1507.  
  1508. after                          01011100
  1509. It can shift the bits toward the left:
  1510. before                         10111001
  1511.  
  1512. after                          01110010
  1513. It can rotate the bits toward the right:
  1514. before                         10111001
  1515.  
  1516. after                          11011100
  1517. It can rotate the bits toward the left:
  1518. before                         10111001
  1519.  
  1520. after                          01110011
  1521. It can shift the bits toward the right arithmetically:
  1522. before                         10111001
  1523.  
  1524. after                          11011100
  1525. It can shift the bits toward the left arithmetically:
  1526. before                         10111001
  1527.  
  1528. after                          11110010
  1529.                              Doubling a number is the same as 
  1530. shifting it left arithmetically. For example, doubling six (to 
  1531. get twelve) is the same as shifting six left arithmetically:
  1532. six                            00000110
  1533.  
  1534. twelve                         00001100
  1535.  
  1536.   Halving a number is the same as shifting it right 
  1537. arithmetically. For example, halving six (to get three) is the 
  1538. same as shifting six right arithmetically:
  1539. six 00000110
  1540.  
  1541. three00000011
  1542. Halving negative six (to get negative three) is the same as 
  1543. shifting negative six right arithmetically:
  1544. negative six11111010
  1545.  
  1546. negative three11111101
  1547.   Using the ALU, the control unit can do operations such as:
  1548. A.Find the number in the 6th memory location, and move its 
  1549. negative to a register.
  1550. B.Change the number in a register, by adding to it the number in 
  1551. the 6th memory location.
  1552. C.Change the number in a register, by subtracting from it the 
  1553. number in the 6th memory location.
  1554.   Most computers require each operation to have one source and 
  1555. one destination. In operations A, B, and C, the source is the 6th 
  1556. memory location; the destination is the register.
  1557.   The control unit cannot do a command such as ``add together the 
  1558. number in the 6th memory location and the number in the 7th 
  1559. memory location, and put the sum in a register'', because that 
  1560. operation would require two sources. Instead, you must give two 
  1561. shorter commands:
  1562. 1. Move the number in the 6th memory location to the register.
  1563. 2. Then add to that register the number in the 7th memory 
  1564. location.
  1565.  
  1566.                       Flags
  1567.   The CPU contains a flag register, which comments on what the 
  1568. CPU is doing. In a typical CPU, the flag register has six bits, 
  1569. named as follows:
  1570. the Negative bit
  1571. the Zero bit
  1572. the Carry bit
  1573. the Overflow bit
  1574. the Priority bit
  1575. the Privilege bit
  1576.   When the CPU performs an operation (such as addition, 
  1577. subtraction, shifting, rotating, or moving), the operation has a 
  1578. source and a destination. The number that goes into the 
  1579. destination is the operation's result. The CPU automatically 
  1580. analyzes that result.
  1581.   Negative bit If the result is a negative number, the CPU turns 
  1582. on the Negative bit. In other words, it makes the Negative bit be 
  1583. 1. (If the result is a number that's not negative, the CPU makes 
  1584. the Negative bit be 0.)
  1585.   Zero bit If the result is zero, the CPU turns on the Zero bit. 
  1586. In other words, it makes the Zero bit be 1.
  1587.   Carry bit When the ALU computes the result, it also computes an 
  1588. extra bit, which becomes the Carry bit.
  1589.   For example, here's how the ALU adds 7 and -4:
  1590.  7 is                  00000111
  1591. -4 is                  11111100
  1592. binary addition gives 100000011
  1593.  
  1594.                         result
  1595.                     Carry
  1596. So the result is 3, and the Carry bit becomes 1.
  1597.   Overflow bit If the ALU can't compute a result correctly, it 
  1598. turns on the Overflow bit.
  1599.   For example, in elementary school you learned that 98+33 is 
  1600. 131; so in binary, the computation should look like this:
  1601.              128 64 32 16 8  4  2  1
  1602. 98 is            1  1  0  0  0  1  0
  1603. 33 is               1  0  0  0  0  1
  1604. the sum is    1  0  0  0  0  0  1  1, which is 131
  1605.  
  1606. But here's what an 8-bit ALU will do:
  1607.             sign 64 32 16 8  4  2  1
  1608. 98 is         0  1  1  0  0  0  1  0
  1609. 33 is         0  0  1  0  0  0  0  1
  1610. the sum is 0  1  0  0  0  0  0  1  1
  1611.  
  1612.          Carry        result
  1613. Unfortunately, the result's leftmost 1 is in the position marked 
  1614. sign, instead of the position marked 128; so the result looks 
  1615. like a negative number.
  1616.                                                      To warn you 
  1617. that the result is incorrect, the ALU turns on the Overflow bit. 
  1618. If you're programming in a language such as BASIC, the 
  1619. interpreter or compiler keeps checking whether the Overflow bit 
  1620. is on; when it finds that the bit's on, it prints the word 
  1621. OVERFLOW.
  1622.                                                      Priority bit 
  1623. While your program's running, it might be interrupted. 
  1624. Peripherals might interrupt, in order to input or output the 
  1625. data; the real-time clock might interrupt, to prevent you from 
  1626. hogging too much time, and to give another program a chance to 
  1627. run; and the computer's sensors might interrupt, when they sense 
  1628. that the computer is malfunctioning.
  1629.                                                      When 
  1630. something wants to interrupt your program, the CPU checks whether 
  1631. your program has priority, by checking the Priority bit. If the 
  1632. Priority bit is on, your program has priority and cannot be 
  1633. interrupted.
  1634.                                                      Privilege 
  1635. bit On a computer that's handling several programs at the same 
  1636. time, some operations are dangerous: if your program makes the 
  1637. computer do those operations, the other programs might be 
  1638. destroyed. Dangerous operations are called privileged 
  1639. instructions; to use them, you must be a privileged user.
  1640.                                                      When you 
  1641. walk up to a terminal attached to a large computer, and type 
  1642. HELLO or LOGIN, and type your user
  1643. number, the operating system examines your user number to find 
  1644. out whether you are a privileged user. If you are, the operating 
  1645. system turns on the Privilege bit. When the CPU starts running 
  1646. your programs, it refuses to do privileged instructions unless 
  1647. the Privilege bit is on.
  1648.                                                      Microcompute
  1649. rs omit the Privilege bit, and can't prevent you from giving 
  1650. dangerous commands. But since the typical microcomputer has only 
  1651. one terminal, the only person your dangerous command can hurt is 
  1652. yourself.
  1653.   Levels of priority & privilege Some computers have several 
  1654. levels of priority and privilege.
  1655.   If your priority level is ``moderately high'', your program is 
  1656. immune from most interruptions, but not from all of them. If your 
  1657. privilege level is ``moderately high'', you can order the CPU to 
  1658. do most of the privileged instructions, but not all of them.
  1659.   To allow those fine distinctions, large computers devote 
  1660. several bits to explaining the priority level, and several bits 
  1661. to explaining the privilege level.
  1662.   Where are the flags? The bits in the flag register are called 
  1663. the flags. To emphasize that the flags comment on your program's 
  1664. status, people sometimes call them status flags.
  1665.   In the CPU, the program counter is next to the flag register. 
  1666. Instead of viewing them as separate registers, some programmers 
  1667. consider them to be parts of a single big register, called the 
  1668. program status word.
  1669.   Tests You can give a command such as, ``Test the 3rd memory 
  1670. location''. The CPU will examine the number in the 3rd memory 
  1671. location. If that number is negative, the CPU will turn on the 
  1672. Negative bit; if that number is zero, the CPU will turn on the 
  1673. Zero bit.
  1674.   You can give a command such as, ``Test the difference between 
  1675. the number in the 3rd register and the number in the 4th''. The 
  1676. CPU will adjust the flags according to whether the difference is 
  1677. negative or zero or carries or overflows.
  1678.   Saying ``if'' The CPU uses the flags when you give a command 
  1679. such as, ``If the Negative bit is on, go do the instruction in 
  1680. memory location 6''.
  1681.                                                  Speed
  1682.                              Computers are fast. To describe 
  1683. computer speeds, programmers use these words:
  1684. Word                               AbbreviationMeaning
  1685. millisecond                        msec or msthousandth of a 
  1686. second; 10-3 seconds
  1687.  
  1688. microsecond                        µsec or µsmillionth of a 
  1689. second; 10-6 seconds
  1690.  
  1691. nanosecond                         nsec or nsbillionth of a 
  1692. second; 10-9 seconds
  1693.  
  1694. picosecond                         psec or pstrillionth of a 
  1695. second; 10-12 seconds
  1696.                              1000 picoseconds is a nanosecond; 
  1697. 1000 nanoseconds is a microsecond; 1000 microseconds is a 
  1698. millisecond; 1000 milliseconds is a second.
  1699.                              Earlier, I explained that the 
  1700. instruction cycle has five steps:
  1701. A.                           Fetch the instruction.
  1702. B.                           Fetch additional parts for the 
  1703. instruction.
  1704. C.                           Update the program counter.
  1705. D.                           Decode the instruction.
  1706. E.                           Execute the instruction.
  1707. The total time to complete the instruction cycle is about a 
  1708. microsecond. The exact time depends on the quality of the CPU, 
  1709. the quality of the main memory, and the difficulty of the 
  1710. instruction, but usually lies between .1 microseconds and 10 
  1711. microseconds.
  1712.                              Here are 5 ways to make the computer 
  1713. act more quickly:
  1714. Method                                 Meaning
  1715. multiprocessing                        The computer holds more 
  1716.                                        than one CPU. (All the 
  1717.                                        CPUs work simultaneously. 
  1718.                                        They share the same main 
  1719.                                        memory. The operating 
  1720.                                        system decides which CPU 
  1721.                                        works on which program. 
  1722.                                        The collection of CPUs is 
  1723.                                        called a multiprocessor.)
  1724.  
  1725. instruction lookahead                  While the CPU is finishing 
  1726.                                        an instruction cycle (by 
  1727.                                        doing steps D and E), it 
  1728.                                        simultaneously begins 
  1729.                                        working on the next 
  1730.                                        instruction cycle (steps A 
  1731.                                        and B).
  1732.  
  1733. array processing                       The CPU holds at least 16 
  1734.                                        ALUs. (All the ALUs work 
  1735.                                        simultaneously. For 
  1736.                                        example, when the control 
  1737.                                        unit wants to solve 16 
  1738.                                        multiplication problems, 
  1739.                                        it sends each problem to a 
  1740.                                        separate ALU; the ALUs 
  1741.                                        compute the products 
  1742.                                        simultaneously. The 
  1743.                                        collection of ALUs is 
  1744.                                        called an array 
  1745.                                        processor.)
  1746.  
  1747. parallel functional units              The ALU is divided into 
  1748.                                        several functional units: 
  1749.                                        an addition unit, a 
  1750.                                        multiplication unit, a 
  1751.                                        division unit, a shift 
  1752.                                        unit, etc. All the units 
  1753.                                        work simultaneously; while 
  1754.                                        one unit is working on one 
  1755.                                        problem, another unit is 
  1756.                                        working on another.
  1757.  
  1758. pipeline architecture                  The ALU (or each ALU 
  1759.                                        functional unit) consists 
  1760.                                        of a ``first stage'' and a 
  1761.                                        ``second stage''. When the 
  1762.                                        control unit sends a 
  1763.                                        problem to the ALU, the 
  1764.                                        problem enters the first 
  1765.                                        stage, then leaves the 
  1766.                                        first stage and enters the 
  1767.                                        second stage. But while 
  1768.                                        the problem is going 
  1769.                                        through the second stage, 
  1770.                                        a new problem starts going 
  1771.                                        through the first stage. 
  1772.                                        (Such an ALU is called a 
  1773.                                        pipeline processor.)
  1774.  
  1775.                                                 Parity
  1776.                              Most large computers put an extra 
  1777. bit at the end of each memory location. For example, a memory 
  1778. location in the PDP-10 holds 36 bits, but the PDP-10 puts an 
  1779. extra bit at the end, making 37 bits altogether. The extra bit is 
  1780. called the parity bit.
  1781.                              If the number of ones in the memory 
  1782. location is even, the CPU turns the parity bit on. If the number 
  1783. of ones in the memory location is odd, the CPU turns the parity 
  1784. bit off.
  1785.                              For example, if the memory location 
  1786. contains these 36 bits ___ 
  1787. 000000000100010000000110000000000000
  1788. there are 4 ones, so the number of ones is even, so the CPU turns 
  1789. the parity bit on:
  1790. 0000000001000100000001100000000000001
  1791.  
  1792.               content             parity
  1793. If the memory location contains these 36 bits instead ___ 
  1794. 000000000100010000000100000000000000
  1795. there are 3 ones, so the number of ones is odd, so the CPU turns 
  1796. the parity bit off:
  1797. 0000000001000100000001000000000000000
  1798.  
  1799.               content             parity
  1800.  
  1801.   Whenever the CPU puts data into the main memory, it also puts 
  1802. in the parity bit. Whenever the CPU grabs data from the main 
  1803. memory, it checks whether the parity bit still matches the 
  1804. content.
  1805.   If the parity bit doesn't match, the CPU knows there was an 
  1806. error, and tries once again to grab the content and the parity 
  1807. bit. If the parity bit disagrees with the content again, the CPU 
  1808. decides that the memory is broken, refuses to run your program, 
  1809. prints a message saying PARITY ERROR, and then sweeps through the 
  1810. whole memory, checking the parity bit of every location; if the 
  1811. CPU finds another parity error (in your program or anyone 
  1812. else's), the CPU shuts off the whole computer.
  1813.   Cheap microcomputers (such as the Apple 2c and Commodore 64) 
  1814. lack parity bits, but the IBM PC has them.
  1815.  
  1816.                        UAL
  1817.   Universal Assembly Language (UAL) is a notation I invented that 
  1818. makes programming in assembly language easier.
  1819.   UAL uses these symbols:
  1820. SymbolMeaning
  1821. M5    the number in the 5th memory location
  1822. R2    the number in the 2nd register
  1823. P     the number in the program counter
  1824.  
  1825. N     the Negative bit
  1826. Z     the Zero bit
  1827. C     the Carry bit
  1828. V     the oVerflow bit
  1829. PRIORITYthe PRIORITY bits
  1830. PRIVILEGEthe PRIVILEGE bits
  1831. F     the content of the entire flag register
  1832.  
  1833. F[5]  the 5th bit in the flag register
  1834. R2[5] the 5th bit in R2
  1835. R2[LEFT]the left half of R2; in other words, the left half of the 
  1836. data in the 2nd register
  1837. R2[RIGHT]the right half of R2
  1838. M5 M6 long number whose left half is in 5th memory location, 
  1839. right half is in 6th location
  1840.   Here are the UAL statements:
  1841. StatementMeaning
  1842. R2=7    Let number in the 2nd register be 7 (by moving 7 into the 
  1843. 2nd register).
  1844. R2=M5   Copy the 5th memory location's contents into the 2nd 
  1845. register.
  1846. R2= = M5Exchange R2 with M5. (Put 5th location's content into 2nd 
  1847. register and vice versa.)
  1848.  
  1849. R2=R2+M5Change the integer in 2nd register, by adding to it the 
  1850. integer in 5th location.
  1851. R2=R2-M5Change the integer in 2nd register, by subtracting the 
  1852. integer in 5th location.
  1853. R2=R2*M5Change the integer in 2nd register, by multiplying it by 
  1854. integer in 5th location.
  1855. R2 REM R3=R2/M5 Change R2, by dividing it by the integer M5. Put 
  1856. division's remainder into R3.
  1857.  
  1858. R2=-M5  Let R2 be the negative of M5.
  1859. R2=NOT M5Let R2 be the one's complement of M5.
  1860. R2=R2 AND M5Change R2, by performing the AND operation.
  1861. R2=R2 OR M5Change R2, by performing the OR operation.
  1862. R2=R2 XOR M5Change R2, by performing the XOR operation.
  1863.  
  1864. SHIFTL R2Shift left.
  1865. SHIFTR R2Shift right.
  1866. SHIFTRA R2Shift right arithmetically.
  1867. SHIFTR3 R2Shift right, 3 times.
  1868. SHIFTR (R7) R2 Shift right, R7 times.
  1869. ROTATEL R2Rotate left.
  1870. ROTATER R2Rotate right.
  1871.  
  1872. TEST R2 Examine number in 2nd register, and adjust flag 
  1873. register's Negative and Zero bits.
  1874. TEST R2-R4Examine the difference between R2 and R4, and adjust 
  1875. the flag register.
  1876.  
  1877. CONTINUENo operation. Just continue on to the next instruction.
  1878. WAIT    Wait until an interrupt occurs.
  1879.  
  1880. IF R2<0, P=7If the number in the 2nd register is negative, put 7 
  1881. into the program counter.
  1882. IF R2<0, M5=3, P=7 If R2<0, do both of the following: let M5 be 
  1883. 3, and P be 7.
  1884.   M5 can be written as M(5) or M(2+3). It can be written as 
  1885. M(R7), if R7 is 5 ___ in other words, if register 7 contains 5.
  1886.  
  1887.                 Addressing modes
  1888.   Suppose you want the 2nd register to contain the number 6. You 
  1889. can accomplish that goal in one step, like this:
  1890. R2=6
  1891. Or you can accomplish it in two steps, like this:
  1892. M5=6
  1893. R2=M5
  1894. Or you can accomplish it in three steps, like this:
  1895. M5=6
  1896. M3=5
  1897. R2=M(M3)
  1898. Or you can accomplish it in an even weirder way:
  1899. M5=6
  1900. R3=1
  1901. R2=M(4+R3)
  1902.                                                      Each of 
  1903. those methods has a name. The first method (R2=6), which is the 
  1904. simplest, is called immediate addressing. The second method 
  1905. (R2=M5), which contains the letter M, is called direct 
  1906. addressing. The third method (R5=M(M3)), which contains the 
  1907. letter M twice, is called indirect addressing. The fourth method 
  1908. (R5=M(4+R3)), which contains the letter M and a plus sign, is 
  1909. called indexed addressing.
  1910.                                                      In each 
  1911. method, the 2nd register is the destination. In the last three 
  1912. methods, the 5th memory location is the source. In the fourth 
  1913. method, which involves R3, the 3rd register is called the index 
  1914. register, and R3 itself is called the index.
  1915.                                                      Each of 
  1916. those methods is called an addressing mode. So you've seen four 
  1917. addressing modes: immediate, direct, indirect, and indexed.
  1918.                                                      Program 
  1919. counter To handle the program counter, the computer uses other 
  1920. addressing modes instead.
  1921.                                                      For example, 
  1922. suppose P (the number in the program counter) is 2073, and you 
  1923. want to change it to 2077. You can accomplish that goal simply, 
  1924. like this:
  1925. P=2077
  1926. Or you can accomplish it in a weirder way, like this:
  1927. P=P+4
  1928. Or you can accomplish it in an even weirder way, like this:
  1929. R3=20
  1930. P=R3 77
  1931.                                                      The first 
  1932. method (P=2077), which is the simplest, is called absolute 
  1933. addressing.
  1934.                                                      The second 
  1935. method (P=P+4), which involves addition, is called relative 
  1936. addressing. The ``+4'' is the offset.
  1937.                                                      The third 
  1938. method (P=R3 77) is called base-page addressing. R3 (which is 20) 
  1939. is called the page number or segment number, and so the 3rd 
  1940. register is called the page register or segment register.
  1941.  
  1942.             8088 DETAILS
  1943.   The first microprocessor (CPU on a chip) was invented by Intel 
  1944. in 1971 and called the Intel 4004. Its accumulator was so short 
  1945. that it held just 4 bits! Later that year, Intel invented an 
  1946. improvement called the Intel 8008, whose accumulator held 8 bits. 
  1947. In 1973 Intel invented a further improvement, called the Intel 
  1948. 8080, which understood more op codes, contained more registers, 
  1949. handled more RAM (64K instead of 16K), and ran faster. Drunk on 
  1950. the glories of that 8080, Microsoft adopted the phone number 
  1951. VAT-8080, and the Boston Computer Society adopted the soberer 
  1952. phone number DOS-8080.
  1953.   In 1978 Intel invented a further improvement, called the 8086, 
  1954. which had a 16-bit accumulator and handled even more RAM & ROM 
  1955. (totalling 1 megabyte). Out of the 8086 came 16 wires (called the 
  1956. data bus), which transmitted 16 bits simultaneously from the 
  1957. accumulator to other computerized devices, such as RAM and disks. 
  1958. Since the 8086 had a 16-bit accumulator and 16-bit data bus, 
  1959. Intel called it a 16-bit CPU.
  1960.   But computerists complained that the 8086 was impractical, 
  1961. since nobody had developed RAM, disks, or other devices for the 
  1962. 16-bit data bus yet. So in 1979 Intel invented the 8088, which 
  1963. understands the same machine language as the 8086 but has an 
  1964. 8-bit data bus. To transmit 16-bit data through the 8-bit bus, 
  1965. the 8088 sends 8 of the bits first, then sends the other 8 bits 
  1966. shortly afterwards. That technique of using a few wires (8) to 
  1967. imitate many (16) is called multiplexing.
  1968.   When 16-bit data buses later became popular, Intel invented a 
  1969. slightly souped-up 8086, called the 80286 (nicknamed the 286).
  1970.   Then Intel invented a 32-bit version called the 80386 
  1971. (nicknamed 386). Intel has also invented a multiplexed version 
  1972. called the 386SX, which understands the same machine language as 
  1973. the 386 but transmits 32-bit data through a 16-bit bus by sending 
  1974. 16 of the bits first and then sending the other 16. The letters 
  1975. ``SX'' mean ``SiXteen-bit bus''. The original 386, which has a 
  1976. 32-bit bus, is called the 386DX; the letters ``DX'' mean ``Double 
  1977. the siXteen-bit bus''.
  1978.   Then Intel invented a slightly souped-up 80386DX, called the 
  1979. 80486 (nicknamed 486). It comes in two versions: the fancy 
  1980. version (called the 486DX) includes a math coprocessor, which is 
  1981. circuitry that understands commands about advanced math; the 
  1982. stripped-down version (called the 486SX) lacks a math 
  1983. coprocessor.
  1984.   Here's how to use the 8088 and 8086. (The 286, 386, and 486 
  1985. include the same features plus more.)
  1986.                                                      Registers
  1987.                                          The CPU contains 
  1988. fourteen 16-bit registers: the accumulator (AX), base register 
  1989. (BX), count register (CX), data register (DX), stack pointer 
  1990. (which UAL calls S but Intel calls SP), base pointer (BP), source 
  1991. index (SI), destination index (DI), program counter (which UAL 
  1992. calls P but Intel calls the instruction pointer or IP), flag 
  1993. register (which UAL calls F), code segment (CS), data segment 
  1994. (DS), stack segment (SS), and extra segment (ES).
  1995.                                          In each of those 
  1996. registers, the sixteen bits are numbered from right to left, so 
  1997. the rightmost bit is called bit 0 and the leftmost bit is called 
  1998. bit fifteen.
  1999.                                          The AX register's 
  2000. low-numbered half (bits 0 through 7) is called A low (or AL). The 
  2001. AX register's high half (bits 8 through fifteen) is called A high 
  2002. (AH).
  2003.                                          In the flag register, 
  2004. bit 0 is the carry flag (which UAL calls C), bit 2 is for parity, 
  2005. bit 6 is the zero flag (Z), bit 7 is the negative flag (which UAL 
  2006. calls N but Intel calls sign or S), bit eleven is the overflow 
  2007. flag (V), bits 4, 8, 9, and ten are special (auxiliary carry, 
  2008. trap, interrupts, and direction), and the remaining bits are 
  2009. unused.
  2010.  
  2011.                                                  Memory locations
  2012.                                          Each memory location 
  2013. contains a byte. In UAL, the 6th memory location is called M6 or 
  2014. M(6). The pair of bytes M7 M6 is called memory word 6, which UAL 
  2015. writes as MW(6).
  2016.  
  2017.                                                   Instruction set
  2018.                                          The next page shows the 
  2019. set of instructions that the 8088 understands. For each 
  2020. instruction, I've given the assembly-language mnemonic and its 
  2021. translation to UAL, where all numbers are hexadecimal.
  2022.                                          The first line says that 
  2023. INC (which stands for INCrement) is the assembly-language 
  2024. mnemonic that means x=x+1. For example, INC AL means AL=AL+1.
  2025.                                          The eighth line says 
  2026. that IMUL (which stands for Integer Multiply) is the 
  2027. assembly-language mnemonic that means x=x*y. For example, IMUL 
  2028. AX,BX means AX=AX*BX.
  2029.                                          In most equations, you 
  2030. can replace the x and y by registers, half-registers, memory 
  2031. locations, numbers, or more exotic entities. To find out what you 
  2032. can replace x and y by, experiment!
  2033.                                          For more details, read 
  2034. the manuals from Intel and Microsoft. They also explain how to 
  2035. modify an instruction's behavior by using flags, segment 
  2036. registers, other registers, and three prefixes: REPeat, SEGment, 
  2037. and LOCK.
  2038. Math
  2039. INCrement   x=x+1
  2040. DECrement   x=x-1
  2041.  
  2042. ADD         x=x+y
  2043. ADd Carry   x=x+y+C
  2044.  
  2045. SUBtract    x=x-y
  2046. SuBtract Borrowx=x-y-C
  2047.  
  2048. MULtiply    x=x*y UNSIGNED
  2049. Integer MULtiplyx=x*y
  2050.  
  2051. DIVide      AX=AX/x UNSIGNED
  2052. Integer DIVideAX=AX/x
  2053.  
  2054. NEGate      x=-x
  2055.  
  2056. Decimal Adjust AddIF AL[RIGHT]>9, AL=AL+6
  2057.             IF AL[LEFT]>9, AL=AL+60
  2058. Decimal Adjust SubtrIF AL[RIGHT]>9, AL=AL-6
  2059.             IF AL[LEFT]>9, AL=AL-60
  2060.  
  2061. Ascii Adjust AddIF AL[RIGHT]>9, AL=AL+6, AH=AH+1
  2062.             AL[LEFT]=0
  2063. Ascii Adjust SubtractIF AL[RIGHT]>9, AL=AL-6, AH=AH-1 AL[LEFT]=0
  2064. Ascii Adjust MultiplyAH REM AL=AL/0A
  2065. Ascii Adjust DivideAL=AL+(0A*AH)
  2066.             AH=0
  2067.  
  2068. Logic
  2069. AND         x=x AND y
  2070. OR          x=x OR y
  2071. XOR         x=x XOR y
  2072. CoMplement CarryC=NOT C
  2073.  
  2074. SHift Left  SHIFTL(y) x
  2075. SHift Right SHIFTR(y) x
  2076. Shift Arithmetic RightSHIFTRA(y) x
  2077.  
  2078. ROtate Left ROTATEL(y) x
  2079. ROtate RightROTATER(y) x
  2080.  
  2081. Rotate Carry LeftROTATEL(y) C x
  2082. Rotate Carry RightROTATER(y) C x
  2083.  
  2084. CLear Carry C=0
  2085. CLear DirectionDIRECTION=0
  2086. CLear InterruptsINTERRUPTS=0
  2087.  
  2088. SeT Carry   C=1
  2089. SeT DirectionDIRECTION=1
  2090. SeT InterruptsINTERRUPTS=1
  2091.  
  2092. TEST        TEST x AND y
  2093. CoMPare     TEST x-y
  2094.  
  2095. SCAn String ByteTEST AL-M(DI); DI=DI+1-(2*DIRECTION)
  2096. SCAn String WordTEST AX-MW(DI); DI=DI+2-(4*DIRECTION)
  2097.  
  2098. CoMPare String ByteTEST M(SI)-M(DI)
  2099.             SI=SI+1-(2*DIRECTION)
  2100.             DI=DI+1-(2*DIRECTION)
  2101. CoMPare String WordTEST MW(SI)-MW(DI)
  2102.             SI=SI+2-(4*DIRECTION)
  2103.             DI=DI+2-(4*DIRECTION)
  2104. Moving bytes
  2105. MOVe                                               x=y
  2106.  
  2107. Load AH from F                                     AH=F[RIGHT]
  2108. Store AH to F                                      F[RIGHT]=AH
  2109.  
  2110. Load register and DS                               x=MW(y); 
  2111. DS=MW(y+2)
  2112. Load register and ES                               x=MW(y); 
  2113. ES=MW(y+2)
  2114.  
  2115. LOaD String Byte                                   AL=M(SI); 
  2116. SI=SI+1-(2*DIRECTION)
  2117. LOaD String Word                                   AX=MW(SI); 
  2118. SI=SI+2-(4*DIRECTION)
  2119.  
  2120. STOre String Byte                                  M(DI)=AL; 
  2121. DI=DI+1-(2*DIRECTION)
  2122. STOre String Word                                  MW(DI)=AX; 
  2123. DI=DI+2-(4*DIRECTION)
  2124.  
  2125. MOVe String Byte                                   M(DI)=M(SI);
  2126.                                                    DI=DI+1-(2*DIR
  2127. ECTION)
  2128.                                                    SI=SI+1-(2*DIR
  2129. ECTION)
  2130. MOVe String Word                                   MW(DI)=MW(SI)
  2131.                                                    DI=DI+2-(4*DIR
  2132. ECTION)
  2133.                                                    SI=SI+2-(4*DIR
  2134. ECTION)
  2135.  
  2136. Convert Byte to Word                               AH=-AL[7]
  2137. Convert Word to Dbl                                DX=-AX[0F]
  2138.  
  2139. PUSH                                               S=S-2; MW(S)=x
  2140. PUSH F                                             S=S-2; MW(S)=F
  2141.  
  2142. POP                                                x=MW(S); S=S+2
  2143. POP F                                              F=MW(S); S=S+2
  2144.  
  2145. IN                                                 x=PORT(y)
  2146. OUT                                                PORT(x)=y
  2147. ESCape                                             BUS=x
  2148.  
  2149. eXCHanGe                                           x= =y
  2150. XLATe                                              AL=M(BX+AL)
  2151. Load Effective Address                             x=ADDRESS(y)
  2152.  
  2153. Program counter
  2154. JuMP                                               P=x
  2155.  
  2156. Jump if Zero                                       IF Z=1, P=x
  2157. Jump if Not Zero                                   IF Z=0, P=x
  2158.  
  2159. Jump if Sign                                       IF N=1, P=x
  2160. Jump if No Sign                                    IF N=0, P=x
  2161.  
  2162. Jump if Overflow                                   IF V=1, P=x
  2163. Jump if Not Overflow                               IF V=0, P=x
  2164.  
  2165. Jump if Parity                                     IF PARITY=1, 
  2166. P=x
  2167. Jump if No Parity                                  IF PARITY=0, 
  2168. P=x
  2169.  
  2170. Jump if Below                                      IF C=1, P=x
  2171. Jump if Above or Eq                                IF C=0, P=x
  2172. Jump if Below or Eq                                IF C=1 OR Z=1, 
  2173. P=x
  2174. Jump if Above                                      IF C=0 AND 
  2175. Z=0, P=x
  2176.  
  2177. Jump if Greater or Eq                              IF N=V, P=x
  2178. Jump if Less                                       IF N<>V, P=x
  2179. Jump if Greater                                    IF N=V AND 
  2180. Z=0, P=x
  2181. Jump if Less or Equal                              IF N<>V OR 
  2182. Z=1, P=x
  2183.  
  2184. Jump if CX Zero                                    IF CX=0, P=x
  2185.  
  2186. LOOP                                               CX=CX-1; IF 
  2187. CX<>0, P=x
  2188. LOOP if Zero                                       CX=CX-1; IF 
  2189. CX<>0 AND Z=1, P=x
  2190. LOOP if Not Zero                                   CX=CX-1; IF 
  2191. CX<>0 AND Z=0, P=x
  2192.  
  2193. CALL                                               S=S-2; 
  2194. MW(S)=P; P=x
  2195. RETurn                                             P=MW(S); S=S+2
  2196.  
  2197. INTerrupt                                          S=S-6; 
  2198. MW(S)=P; MW(S+2)=CS; MW(S+4)=F P=MW(4*x); CS=MW(4*x+2)
  2199.                                                    INTERRUPTS=0; 
  2200. TRAP=0
  2201. INTerrupt if Overflow                              IF V=1, S=S-6, 
  2202. MW(S)=P, MW(S+2)=CS,
  2203.                                                    MW(S+4)=F, 
  2204. P=MW(10), CS=MW(12),
  2205.                                                    INTERRUPTS=0, 
  2206. TRAP=0
  2207. Interrupt RETurn                                   P=MW(S); 
  2208. CS=MW(S+2); F=MW(S+4); S=S+6
  2209.  
  2210. No OPeration                                       CONTINUE
  2211. HaLT                                               WAIT
  2212. WAIT                                               WAIT FOR 
  2213. COPROCESSOR
  2214.